-
Notifications
You must be signed in to change notification settings - Fork 0
/
TransformExpression.java
44 lines (41 loc) · 1.07 KB
/
TransformExpression.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
/* https://www.spoj.com/problems/ONP/
#stack
*/
import java.util.Deque;
import java.util.LinkedList;
import java.util.Scanner;
class TransformExpression {
static void transform(String str) {
Deque<Character> stack = new LinkedList<Character>();
StringBuilder result = new StringBuilder();
for (char c : str.toCharArray()) {
if (c < 97) { // "a"
if (c != 41) { // ")"
stack.addFirst(c);
} else {
while (!stack.isEmpty() && stack.peekFirst() != 40) { // "("
result.append(stack.removeFirst());
}
if (!stack.isEmpty() && stack.peekFirst() == 40) { // "("
stack.removeFirst();
}
}
} else {
result.append(c);
}
}
System.out.println(result);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
String[] arr = new String[n];
for (int i = 0; i < n; i++) {
String str = sc.next();
arr[i] = str;
}
for (String str : arr) {
transform(str);
}
}
}