알고리즘 풀이/백준

[백준][Java] 10799번 쇠막대기

배게 2019. 3. 24. 10:12
728x90


 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
33
34
import java.util.*;



public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String s = sc.nextLine();
		
		Stack<Character> stack = new Stack<>();
		
		int result=0;
		for(int i=0; i<s.length(); i++) {
			if(s.charAt(i)=='(') {
				stack.push('(');
			}
			else if(s.charAt(i)==')') {
				if(s.charAt(i-1)=='(') {
					stack.pop();
					result += stack.size();
				}
				else {
					stack.pop();
					result+=1;
				}
			}
		}
		
		System.out.println(result);
		
	 
	}
}