[SWEA] 5432. 쇠막대기 자르기 [D4]
📚 문제
표로 나타내어 어떤 패턴이 있는지 확인해본다. cnt는 괄호가 열리면 +1, 닫히면 -1을 해준다.
( | ) | ( | ( | ( | ( | ) | ( | ) | ) | ( | ( | ) | ) | ( | ) | ) | ) | ( | ( | ) | ) | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
cnt | 1 | 0 | 1 | 2 | 3 | 4 | 3 | 4 | 3 | 2 | 3 | 4 | 3 | 2 | 3 | 2 | 1 | 0 | 1 | 2 | 1 | 0 |
result | 0 | 3 | 3 | 1 | 3 | 1 | 2 | 1 | 1 | 1 | 1 |
(
가 나오면cnt += 1
)
가 나오면cnt -= 1
)
다음에)
가 연속으로 나오면result += 1
(
다음에)
가 나오면 `result += cnt'
result를 다 더하면 17로 정상적으로 출력된다.
📒 코드
T = int(input())
for tc in range(1, T + 1):
gwalho = ' ' + input()
cnt = 0
result = 0
for i in range(1, len(gwalho)):
if gwalho[i] == '(': # cnt 1 증가
cnt += 1
else:
cnt -= 1
if gwalho[i-1] == '(':
result += cnt
else: result += 1
print(f'#{tc} {result}')
🔍 결과 : Pass
Author And Source
이 문제에 관하여([SWEA] 5432. 쇠막대기 자르기 [D4]), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@yunhlim/SWEA-5432.-쇠막대기-자르기-D4저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)