본문 바로가기
알고리즘/SWEA

[파이썬] [SWEA] [파이썬 S/W 문제해결 기본] 4866. 4일차 - 괄호검사

by SBOX Learning by doing 2021. 2. 25.
반응형

swexpertacademy.com/main/learn/course/subjectList.do?courseId=AVuPDN86AAXw5UW6

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

swexpertacademy.com

T = int(input())

for tc in range(1, T+1):
    tmp = str(input())
    tmp_list = []
    for i in range(len(tmp)):

        # 열기
        if tmp[i] == '(':
            tmp_list.append('(')

        if tmp[i] == '{':
            tmp_list.append('{')
        # 닫기
        if tmp[i] == ')':
            if not tmp_list:
                tmp_list.append(')')
            else:
                if tmp_list[-1] == '(':
                    tmp_list.pop()
                else:
                    tmp_list.append(')')

        if tmp[i] == '}':
            if not tmp_list:
                tmp_list.append('}')
            else:
                if tmp_list[-1] == '{':
                    tmp_list.pop()
                else:
                    tmp_list.append('}')
    if not tmp_list:
        print("#{} {}".format(tc, 1))
    else:
        print("#{} {}".format(tc, 0))
반응형

댓글