문제 보기 >> 5432. 쇠막대기 자르기




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
35
36
37
38
39
40
41
42
43
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;
 
public class Solution_5432_쇠막대기자르기 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int T = Integer.parseInt(br.readLine());
        
        for(int tc = 1 ;tc<= T ;tc++) {
            String str = br.readLine();
            char now;
            char[] arr = new char[str.length()];
            int piece = 0;
            int stick=0;
            for(int i=0;i<str.length();i++) {
                now = str.charAt(i);
                
                if(now==')') {
                    char pre = str.charAt(i-1);
                    if(pre == '(') { //레이저
                        stick--;
                        piece += stick;
                    }
                    else {
                        stick --;
                        piece++;
                    }
                }
                
                if(now == '(') {
                    stick++;
                }
                
            }
        
        
            System.out.println("#"+tc+" "+piece);
        }//testcase end
    }// end of main
}
 
cs


+ Recent posts