문제 출처: 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




'Algorithm' 카테고리의 다른 글

백준 2504번: 괄호의 값  (0) 2018.08.01
백준 1874번: 스택 수열  (0) 2018.07.23
백준 9012번: 괄호  (0) 2018.07.18
백준 10828번: 스택  (0) 2018.07.17
백준 1260번: DFS와 BFS  (0) 2018.04.18

+ Recent posts