728x90
반응형
기본적인 스택 문제이다.
내가 스택 맨 처음 공부할 때 봤던 것이 후위 표기식 구현 코드였는데 이렇게 문제로 보니 반갑다!
li = list(input())
op = {'(': 0, '+':1, '-': 1, '*': 2, '/': 2, ')': 3}
stack, output = [], []
for c in li:
if c not in op:
output.append(c)
elif c == '(':
stack.append(c)
elif c == ')':
while stack and stack[-1] != '(':
output.append(stack.pop())
stack.pop()
else:
while stack and op[stack[-1]] >= op[c]:
output.append(stack.pop())
stack.append(c)
while stack:
output.append(stack.pop())
print(''.join(output))
728x90
반응형
'Agorithm > 백준 알고리즘' 카테고리의 다른 글
백준 알고리즘 1254번 팰린드롬 만들기(python) (0) | 2021.03.07 |
---|---|
백준 알고리즘 7785번 회사에 있는 사람(python) (0) | 2021.03.07 |
백준 알고리즘 2493번 탑(python) (0) | 2021.03.07 |
백준 알고리즘 10799번 쇠막대기(python) (0) | 2021.03.07 |
백준 알고리즘 13015번 별 찍기 - 23(python) (0) | 2021.03.07 |