-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy paththegrandadventure.java
50 lines (48 loc) · 1.16 KB
/
thegrandadventure.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
import java.util.*;
import java.math.*;
import java.io.*;
public class thegrandadventure {
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
int n = Integer.parseInt(in.readLine());
while(n-- > 0) {
String line = in.readLine().replace(".", "");
Stack<Character> stack = new Stack<Character>();
boolean status = true;
for(char c : line.toCharArray()) {
if(!status) break;
if(c == '$' || c == '|' || c == '*')
stack.push(c);
else {
if(stack.isEmpty()) {
out.println("NO");
status = false;
break;
}
char x = stack.pop();
if(c == 'b' && x != '$') {
out.println("NO");
status = false;
}
if(c == 't' && x != '|') {
out.println("NO");
status = false;
}
if(c == 'j' && x != '*') {
out.println("NO");
status = false;
}
}
}
if(!stack.isEmpty() && status) {
out.println("NO");
status = false;
}
if(status) {
out.println("YES");
}
}
out.close();
}
}