https://www.acmicpc.net/problem/4889
import sys
input = sys.stdin.readline
cnt = 0
while True:
s = input().strip()
if '-' in s:
break
stack = list()
cnt += 1
# 기존에 있는 안정적인 문자열 제거
for sc in s:
if len(stack) == 0:
stack.append(sc)
else:
if sc == '}' and stack[-1] == '{':
stack.pop()
else:
stack.append(sc)
openS = 0
closeS = 0
for ss in stack:
if ss == '{':
openS += 1
else:
closeS += 1
# 필요한 연산 수 = (openS // 2) + (closeS // 2) + (openS % 2 + closeS % 2)
# Ex. 3개, 3개가 남은 경우 - }}}{{{ 각각 한쌍씩 +2, 나머지 2개 +2 = 4
ops = (openS // 2) + (closeS // 2) + (openS % 2 + closeS % 2)
print(f"{cnt}. {ops}")'코딩테스트 > 코딩테스트 문제풀이' 카테고리의 다른 글
| [Python] 백준 <1986-체스> (0) | 2025.06.24 |
|---|---|
| [Python] 백준 <1309-동물원> (0) | 2025.06.22 |
| [Python] 백준 <2792-보석 상자> (0) | 2025.06.21 |
| [Python] 백준 <9375-해적왕 신해빈> (0) | 2025.06.11 |
| [Python] 백준 <1966-프린터 큐> (0) | 2025.06.11 |