https://www.acmicpc.net/problem/2504
문제
정답 풀이
import sys
str = sys.stdin.readline().rstrip()
stack = list()
for i in str:
if i == ")":
temp = 0
while stack: # stack에 element가 있다면
top = stack.pop()
if top == "(":
if temp == 0:
stack.append(2)
else:
stack.append(2 * temp)
break
elif top == "[": # ([)] 같은 경우는 올바르지 못한 괄호열이기에
print("0") # 0을 출력하고
exit(0) # 프로그램을 바로 종료시킨다
else: # 숫자가 나온다면
if temp == 0:
temp = int(top) # temp를 업데이트한다
else:
temp = temp + int(top)
elif i == "]":
temp = 0
while stack:
top = stack.pop()
if top == "[":
if temp == 0:
stack.append(3)
else:
stack.append(3*temp)
break
elif top == "(":
print(0)
exit(0)
else:
if temp == 0:
temp = int(top)
else:
temp = temp + int(top)
else:
stack.append(i)
result = 0
#마지막에 숫자만 남았을때 서로 더해줘서 결과값을 만든다.
for i in stack:
if i == "(" or i == "[":
print(0)
exit(0)
else:
result += i
print(result)
출처:https://chocodrogba.tistory.com/9
해석
'sw사관학교 정글 2기 > 02 이분탐색, 분할정복, 스택, 큐, 우선순위 큐' 카테고리의 다른 글
[이분탐색] 백준 8983번 사냥꾼 with Python3 ★ (0) | 2021.08.17 |
---|---|
[스택] 백준 2812번 크게 만들기 with Python3 (0) | 2021.08.16 |
[분할정복] 백준 10830번 행렬 제곱 with Python3 ★★ (0) | 2021.08.16 |
[이분탐색] 백준 11053번 가장 긴 증가하는 부분 수열 with Python3 ★ (0) | 2021.08.16 |
[우선순위 큐, 힙] 백준 1715번 카드 정렬하기 with Python3 (0) | 2021.08.16 |
댓글