-
Notifications
You must be signed in to change notification settings - Fork 0
/
673.java
104 lines (86 loc) · 2.38 KB
/
673.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package apr;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.Stack;
public class Main {
public static void main(String[] args) throws Exception {
try {
new uva().new ProblemSolution().solution();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
class uva {
public class ProblemSolution {
private int testCase;
private String str, str2;
private Stack<Character> stack = new Stack<>();
public void solution() throws Exception {
str2 = scanner.readLine();
testCase = Integer.parseInt(str2);
for (int test = 1; test <= testCase; test++) {
str = "";
str = scanner.readLine();
// out.println(str);
if (str == null) {
out.println("Yes");
continue;
}
if (str.length() == 1) {
out.println("No");
continue;
}
stack.clear();
boolean Test = false;
int i;
for (i = 0; i < str.length(); i++) {
if (str.charAt(i) == '(' || str.charAt(i) == '[') {
// out.println(str.charAt(i));
stack.push(str.charAt(i));
} else {
if (str.charAt(i) == ')' && !stack.isEmpty() && stack.peek() == '(') {
stack.pop();
} else {
if (str.charAt(i) == ']' && !stack.isEmpty() && stack.peek() == '[') {
stack.pop();
} else {
if (str.charAt(i) == '(' || str.charAt(i) == ')' || str.charAt(i) == '['
|| str.charAt(i) == ']') {
out.println("No");
Test = true;
break;
}
}
}
}
}
if (Test)
continue;
if (stack.isEmpty() && i >= str.length()) {
out.println("Yes");
} else {
out.println("No");
}
}
scanner.close();
}
}
/*
* output class
*/
// private DataOutputStream out = new DataOutputStream(System.out);
private PrintWriter out = new PrintWriter(System.out, true);
/*
* input class
*/
// private DataInputStream scanner =new DataInputStream(System.in);
private BufferedReader scanner = new BufferedReader(new InputStreamReader(System.in));
// private Scanner scanner = new Scanner(System.in);
// private Scanner scanner = fileFactory.scanner();
// private BufferedReader scanner = fileFactory.bufferedRader();
}