문제

입출력 예시

💡 사고의 흐름
- 여는 괄호일 때, stack에 push
- 닫는 괄호일 때, stack.pop()
- stack.pop()한 값이 여는 괄호라면,
tmp==0 일 때, ()->2, []->3 값을 push
tmp!=0이라면 계산된 결과를 push
- stack에 값이 있다면 각자의 짝에 맞는 여는 괄호가 나올 때 까지 실행 stack.pop()한 값이 숫자라면 -> tmp+=int(top)을 해주어 숫자 합을 구해줌.
--> 따라서, stack에 계산된 값을 넣고는 break를 해주어 반복문을 빠져나올 수 있도록 함.
Code
import sys
vps = sys.stdin.readline()
stack=[]
for v in vps:
if v=='(' or v=='[':
stack.append(v)
elif v==')':
tmp=0
if len(stack)==0:
print(0)
exit()
while len(stack)>0:
top = stack.pop()
if top=='(':
if tmp==0:
stack.append(2)
else:
stack.append(tmp*2)
break
elif top=='[':
print(0)
exit()
else:
tmp +=int(top)
elif v==']':
tmp=0
if len(stack)==0:
print(0)
exit()
while len(stack)>0:
top = stack.pop()
if top=='[':
if tmp==0:
stack.append(3)
else:
stack.append(3*tmp)
break
elif top=='(':
print(0)
exit()
else:
tmp+=int(top)
answer=0
for s in stack:
if s=='(' or s=='[':
print(0)
exit()
else:
answer+=s
print(answer)