Skip to content

Commit 08be828

Browse files
authored
[20250710] BOJ / G2 / 후위 표기식 / 권혁준
1 parent 59d7723 commit 08be828

File tree

1 file changed

+131
-0
lines changed

1 file changed

+131
-0
lines changed
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
```java
2+
import java.util.*;
3+
import java.io.*;
4+
5+
class IOController {
6+
BufferedReader br;
7+
BufferedWriter bw;
8+
StringTokenizer st;
9+
10+
public IOController() {
11+
br = new BufferedReader(new InputStreamReader(System.in));
12+
bw = new BufferedWriter(new OutputStreamWriter(System.out));
13+
st = new StringTokenizer("");
14+
}
15+
16+
String nextLine() throws Exception {
17+
String line = br.readLine();
18+
st = new StringTokenizer(line);
19+
return line;
20+
}
21+
22+
String nextToken() throws Exception {
23+
while (!st.hasMoreTokens()) nextLine();
24+
return st.nextToken();
25+
}
26+
27+
int nextInt() throws Exception {
28+
return Integer.parseInt(nextToken());
29+
}
30+
31+
long nextLong() throws Exception {
32+
return Long.parseLong(nextToken());
33+
}
34+
35+
double nextDouble() throws Exception {
36+
return Double.parseDouble(nextToken());
37+
}
38+
39+
void close() throws Exception {
40+
bw.flush();
41+
bw.close();
42+
}
43+
44+
void write(String content) throws Exception {
45+
bw.write(content);
46+
}
47+
48+
}
49+
50+
public class Main {
51+
52+
static IOController io;
53+
54+
//
55+
56+
static char[] a;
57+
58+
public static void main(String[] args) throws Exception {
59+
60+
io = new IOController();
61+
62+
init();
63+
solve();
64+
65+
io.close();
66+
}
67+
68+
public static void init() throws Exception {
69+
70+
a = io.nextLine().toCharArray();
71+
72+
}
73+
74+
static void solve() throws Exception {
75+
76+
Deque<String> d = new ArrayDeque<>();
77+
for(int i=0;i<a.length;i++) d.offerLast(Character.toString(a[i]));
78+
io.write(get(d));
79+
80+
}
81+
82+
static String get(Deque<String> d) {
83+
Deque<String> d1 = new ArrayDeque<>();
84+
while(!d.isEmpty()) {
85+
String a = d.pollFirst();
86+
if(a.equals(")")) {
87+
String last;
88+
Deque<String> temp = new ArrayDeque<>();
89+
while(!(last = d1.pollLast()).equals("(")) temp.offerFirst(last);
90+
d1.addLast(get(temp));
91+
}
92+
else d1.addLast(a);
93+
}
94+
95+
Deque<String> d2 = new ArrayDeque<>();
96+
while(!d1.isEmpty()) {
97+
String a = d1.pollFirst();
98+
if(d2.isEmpty()) {
99+
d2.addLast(a);
100+
continue;
101+
}
102+
String last = d2.peekLast();
103+
if(last.equals("*") || last.equals("/")) {
104+
d2.pollLast();
105+
String llast = d2.pollLast();
106+
d2.addLast(llast + a + last);
107+
}
108+
else d2.addLast(a);
109+
}
110+
111+
Deque<String> d3 = new ArrayDeque<>();
112+
while(!d2.isEmpty()) {
113+
String a = d2.pollFirst();
114+
if(d3.isEmpty()) {
115+
d3.addLast(a);
116+
continue;
117+
}
118+
String last = d3.peekLast();
119+
if(last.equals("+") || last.equals("-")) {
120+
d3.pollLast();
121+
String llast = d3.pollLast();
122+
d3.addLast(llast + a + last);
123+
}
124+
else d3.addLast(a);
125+
}
126+
127+
return d3.poll();
128+
}
129+
130+
}
131+
```

0 commit comments

Comments
 (0)