[백준] 26111 : Parentheses Tree

백지원·2023년 9월 20일
0

정답코드

import sys
input = sys.stdin.readline

x = input().rstrip()
now = 0
ans = 0

for i in range(len(x)-1):
    s = x[i:i+2]
    if s == "()": # leaf 노드의 깊이를 ans에 더함
        ans += now
    elif s == "((": # 깊이가 깊어짐
        now += 1
    elif s == "))": # 깊이가 얕아짐
        now -= 1
print(ans)

풀이

주석 설명

0개의 댓글