-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJ08027_GoBanPhim.java
44 lines (41 loc) · 1.21 KB
/
J08027_GoBanPhim.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
import java.io.*;
import java.math.*;
import java.util.*;
public class J08027_GoBanPhim {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.next();
System.out.println(ptit(s));
}
public static String ptit(String s) {
int n = s.length();
Stack<Character> st = new Stack<>();
Stack<Character> sc = new Stack<>();
for (int j = 0; j < n; j++) {
char i = s.charAt(j);
if (i == '<') {
if (!sc.empty()) {
st.push(sc.pop());
}
} else if (i == '>') {
if (!st.empty()) {
sc.push(st.pop());
}
} else if (i == '-') {
if (!sc.empty()) {
sc.pop();
}
} else {
sc.push(i);
}
}
StringBuilder sb = new StringBuilder("");
while (!sc.empty()) {
st.push(sc.pop());
}
for (int i = st.size() - 1; i >= 0; i--) {
sb.insert(sb.length(), st.elementAt(i));
}
return sb.toString();
}
}