Repository navigation
Expand file tree
/
Copy pathSolution1218.java
More file actions
54 lines (52 loc) · 1.53 KB
/
Copy pathSolution1218.java
File metadata and controls
54 lines (52 loc) · 1.53 KB
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
package swea.stack1;
import java.util.Scanner;
public class Solution1218 {
static int n,a,b,c,d;
static String s;
static boolean flag;
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
for (int i = 1; i < 11; i++) {
n=sc.nextInt();
s=sc.next();
a=b=c=d=0;
flag=false;
for (int j = 0; j < n; j++) {
char m = s.charAt(j);
if (m=='(' | m==')'){
a+=(m=='('?1:-1);
if (a<0){
flag=true;
break;
}
}else if(m=='[' | m==']'){
b+=(m=='['?1:-1);
if (b<0){
flag=true;
break;
}
}else if(m=='{' | m=='}'){
c+=(m=='{'?1:-1);
if (c<0){
flag=true;
break;
}
}else if(m=='<' | m=='>'){
d+=(m=='<'?1:-1);
if (d<0){
flag=true;
break;
}
}
}
if (flag) {
System.out.printf("#%d %d%n",i,0);
}
else if (a==0 &&b==0&&c==0&&d==0){
System.out.printf("#%d %d%n",i,1);
}else{
System.out.printf("#%d %d%n",i,0);
}
}
}
}