-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path5397.java
73 lines (63 loc) · 1.51 KB
/
5397.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(reader.readLine());
MyStack front = new MyStack(1000001);
MyStack back = new MyStack(1000001);
for(int testCase = 0 ; testCase< T; testCase++) {
String commend = reader.readLine();
for(int i=0, size = commend.length(); i < size; i++) {
switch(commend.charAt(i)) {
case '<':back.push(front.pop());
break;
case '>':front.push(back.pop());
break;
case '-':front.pop();
break;
default :front.push(commend.charAt(i));
break;
}
}
System.out.print(front.toString());
System.out.println(back.toReverseString());
front.clear();
back.clear();
}
}
static class MyStack{
char[] stack;
int size;
public MyStack(int size) {
stack = new char[size];
this.size = 0;
}
public void push(char word) {
if(word != ' ') {
stack[size++] = word;
}
}
public char pop() {
if(size == 0) {
return ' ';
}else {
return stack[--size];
}
}
public String toString() {
return String.valueOf(stack, 0, size);
}
public String toReverseString() {
char[] reverse = new char[size];
int lastIdx = size-1;
for(int i = 0; i < size; i++) {
reverse[i] = stack[lastIdx-i];
}
return String.valueOf(reverse);
}
public void clear() {
size = 0;
}
}
}