Algorithm
백준 10799번: 쇠막대기
qlyh8
2018. 7. 20. 02:49
문제 출처: https://www.acmicpc.net/problem/10799
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Stack; public class algo5_10799_iron_stick { public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); String input = reader.readLine(); Stack<Character> stack = new Stack<>(); int number = 0; for(int i=0 ; i<input.length() ; i++){ if(input.charAt(i)=='(' && input.charAt(i+1)==')'){ number += stack.size(); ++i; } else if(input.charAt(i)==')'){ stack.pop(); ++number; } else{ stack.push(input.charAt(i)); } } System.out.println(number); } } | cs |