Contents

BEAKJOON 1918, , ,

1918_후위 표기식

 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
import sys
from collections import deque

text = sys.stdin.readline().rstrip()

# 후위 표기법 알고리즘
operator = {"+":1, "-":1, "*":2 , "/":2, "(":0, ")":0}
stack = deque()
result = ""

for i in text:
    if i not in operator:
        result += i
    elif i == "(":
        stack.append(i)
    elif i == ")":
        while stack and stack[-1] != "(":
            result += stack.pop()
        stack.pop()
    else:
        while stack and operator[i] <= operator[stack[-1]]:
            result += stack.pop()
        stack.append(i)

while stack:
    result += stack.pop()
                
print(result)