Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 29 | 30 | 31 |
Tags
- 가상메모리 관리
- DP
- 딕셔너리
- 스택
- level0
- MYSQL
- python
- level1
- 다이나믹 프로그래밍
- dfs
- 파이썬
- 재귀
- 가상메모리
- level2
- 힙
- 그리디
- dict
- 브루트포스
- BOJ
- 백준
- N과M
- 다익스트라
- programmers
- 구현
- 프로그래머스
- 코딩테스트
- level3
- BFS
- 운영체제
- 수학
Archives
- Today
- Total
동캄의 코딩도장
백준 2504 [괄호의 값] 파이썬 본문
https://www.acmicpc.net/problem/2504
2504번: 괄호의 값
4개의 기호 ‘(’, ‘)’, ‘[’, ‘]’를 이용해서 만들어지는 괄호열 중에서 올바른 괄호열이란 다음과 같이 정의된다. 한 쌍의 괄호로만 이루어진 ‘()’와 ‘[]’는 올바른 괄호열이다. 만일
www.acmicpc.net
# 백준 2504 괄호의 값
import sys
input = sys.stdin.readline
lst = list(map(str, input().rstrip()))
stack = []
ans = 0
temp = 1
for i in range(len(lst)):
if lst[i] == '(':
stack.append(lst[i])
temp *= 2
elif lst[i] == '[':
stack.append(lst[i])
temp *= 3
elif lst[i] == ')':
if not stack or stack[-1] == '[':
ans = 0
break
if lst[i-1] == '(':
ans += temp
stack.pop()
temp //= 2
else:
if not stack or stack[-1] == '(':
ans = 0
break
if lst[i-1] == '[':
ans += temp
stack.pop()
temp //= 3
if stack:
print(0)
else:
print(ans)
스택을 이용 --> 스택+재귀를 이용 하려고 하였으나 해결하지 못하였다.
다른 분의 풀이를 참고하였다.
'코테 > BOJ' 카테고리의 다른 글
백준 2800 [괄호 제거] 파이썬 (0) | 2022.09.07 |
---|---|
백준 2491[수열] 파이썬 (0) | 2022.09.07 |
백준 1935 [후위 표기식2] 파이썬 (0) | 2022.09.07 |
백준 9507 [Generations of Tribbles] 파이썬 (0) | 2022.09.05 |
백준 2407 [조합] 파이썬 (0) | 2022.09.05 |