forked from improper4/uva
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UVa11988_BrokenKeyboardakaBeijuText.java
56 lines (49 loc) · 1.39 KB
/
UVa11988_BrokenKeyboardakaBeijuText.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
package uva;
/* USER: 46724 (sfmunera) */
/* PROBLEM: 3139 (11988 - Broken Keyboard (a.k.a. Beiju Text)) */
/* SUBMISSION: 10101489 */
/* SUBMISSION TIME: 2012-05-11 15:20:56 */
/* LANGUAGE: 2 */
import java.util.*;
import java.io.*;
public class UVa11988_BrokenKeyboardakaBeijuText {
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String line;
while ((line = in.readLine()) != null) {
char[] stream = line.toCharArray();
boolean begin = false;
StringBuilder cur = new StringBuilder();
Deque<String> total = new ArrayDeque<String>();
for (char c : stream) {
if (c == '[') {
if (begin)
total.offerFirst(cur.toString());
else
total.offerLast(cur.toString());
cur = new StringBuilder();
begin = true;
} else if (c == ']') {
if (begin)
total.offerFirst(cur.toString());
else
total.offerLast(cur.toString());
cur = new StringBuilder();
begin = false;
} else {
cur.append(c);
}
}
if (begin)
total.offerFirst(cur.toString());
else
total.offerLast(cur.toString());
StringBuilder res = new StringBuilder();
while (!total.isEmpty())
res.append(total.pollFirst());
System.out.println(res.toString());
}
in.close();
System.exit(0);
}
}