본문 바로가기
코딩테스트/SWExpertAcademy

[Python] SWEA 13760 - 쇠막대기 자르기

by Ssubini 2022. 4. 13.

[2022.02.17]

> 풀이방법

총 개수 : 레이저 지나갈 때 + 막대수 , 괄호 닫을 때 +1

막대 : 괄호 열릴 때 + 1 , 괄호 닫을 때 -1

 

T = int(input())
for tc in range(T):
    stick = input()
    total = 0
    cstick = 0

    for s in range(len(stick)):
        if stick[s] == '(':
            if stick[s+1] == '(':
                cstick +=1
            else:
                # 레이저 지나갈 때
                total += cstick
        else:
            if stick[s-1] == ')':
                total+=1
                cstick-=1
    print(f'#{tc+1} {total}')

 

댓글