-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1218.java
60 lines (59 loc) · 1.23 KB
/
1218.java
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;
public class Solution {
static Stack<Character> stack;
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int size;
char [] input;
char top;
int i,result;
for(int t=0;t<10;t++) {
size=Integer.parseInt(br.readLine());
input=br.readLine().toCharArray();
stack= new Stack<>();
result=1;
loop: for(i=0;i<size;i++) {
if(input[i]=='(' || input[i]=='['||input[i]=='{'||input[i]=='<') {
stack.add(input[i]);
}
else {
if(stack.empty()) {
result=0;
break loop;
}
top=stack.pop();
switch(top) {
case '(':
if(input[i]!=')') {
result=0;
break loop;
}
break;
case '[':
if(input[i]!=']') {
result=0;
break loop;
}
break;
case '{':
if(input[i]!='}') {
result=0;
break loop;
}
break;
case '<':
if(input[i]!='>') {
result=0;
break loop;
}
}
}
}
System.out.print("#"+(t+1)+" ");
System.out.println(result);
}
}
}