import java.util.Scanner;
import java.util.Stack;
 
class Solution {
 
    public static void main(String args[]) throws Exception {
 
        Scanner sc = new Scanner(System.in);
        int[] pair = new int[256]; // 아스키코드
         
        pair['('] = ')';
        pair['['] = ']';
        pair['{'] = '}';
        pair['<'] = '>';
         
        for (int T = 1; T <= 10; T++) {
 
            Stack<Character> stack = new Stack();
            boolean possible = true;
 
            // [input] Test 번호
            int inputLen = Integer.parseInt(sc.nextLine());
             
            // [input] 데이터 입력 & 풀이
            String line = sc.nextLine();
            for (int i = 0; i < inputLen; i++) {
                 
                char input = line.charAt(i);
                // 괄호시작
                if(input == '(' ||input == '[' ||input == '{' ||input == '<')
                {
                    stack.push(input);
                }
                 
                // 괄호끝
                if(input == ')' ||input == ']' ||input == '}' ||input == '>')
                {
                    if(stack.empty()) {
                        possible = false;
                        break;
                    }
                     
                    if(pair[stack.pop().charValue()] != input) {
                        possible = false;
                        break;
                    }
                }
            }
 
            System.out.println(String.format("#%d %d", T, possible?1:0));
        }
 
    }
 
}


+ Recent posts