-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboj1406.java
More file actions
41 lines (34 loc) · 1.23 KB
/
boj1406.java
File metadata and controls
41 lines (34 loc) · 1.23 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
import java.io.*;
import java.util.Stack;
public class boj1406 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
String str = br.readLine();
int M = Integer.parseInt(br.readLine());
Stack<Character> lstack = new Stack<>();
Stack<Character> rstack = new Stack<>();
for (int i = 0; i < str.length(); i++) {
lstack.push(str.charAt(i));
}
while (M-- > 0) {
String[] word = br.readLine().split(" ");
if (word[0].equals("L") && !lstack.isEmpty()) {
rstack.push(lstack.pop());
} else if (word[0].equals("D") && !rstack.isEmpty()) {
lstack.push(rstack.pop());
} else if (word[0].equals("B") && !lstack.isEmpty()) {
lstack.pop();
} else if (word[0].equals("P")) {
lstack.push(word[1].charAt(0));
}
}
while (!lstack.isEmpty()) {
rstack.push(lstack.pop());
}
while (!rstack.isEmpty()) {
sb.append(rstack.pop());
}
System.out.println(sb);
}
}