[알고리즘/SWEA] #5432 쇠막대기 자르기

package algorithm_lab.day06.sup;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Solution {
	public static void main(String[] args) throws NumberFormatException, IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringBuilder sb = new StringBuilder();
		
		int test_case=Integer.parseInt(br.readLine());
		
		for(int t=1;t<=test_case;t++) {
			
			int a=0;
			int cnt=0;
			char[] chars = br.readLine().toCharArray();
			
			for(int i=0;i<chars.length;i++) {
				if(chars[i]==')'&&i-1>=0&&chars[i-1]!='(') {
					cnt+=1;
                    a-=1;
				}
				if(chars[i]=='('&&i+1<chars.length&&chars[i+1]!=')') {
					a+=1;
				}
				if(chars[i]=='('&&i+1<chars.length&&chars[i+1]==')') {
					cnt+=a;
				}
			}
			sb.append("#").append(t).append(" ").append(cnt).append("\n");
		}
		System.out.print(sb.toString());
	}
}

좋은 웹페이지 즐겨찾기