[BOJ] 4949 균형잡힌 세상

nerry·2022년 1월 13일
0

알고리즘

목록 보기
7/86

4949 균형잡힌 세상

import sys
lines = sys.stdin.readline()
while lines!='.\n':
    stack=[]
    lines=list(lines)
    for l in lines:
        if l=='.': break
        if not (l.isalpha() or l==' '):
            if stack:
                if l =='(' or l=='[':stack.append(l)
                else:
                    if stack[-1]=='(':
                        if not l==')':break
                        elif l==')':stack.pop()
                    else:
                        if not l==']':break
                        elif l==']':stack.pop()
            else:
                stack.append(l)
                if l==')' or l==']':break
    if not stack: print('yes')
    else : print('no')
    lines=sys.stdin.readline()
while True :
    a = input()
    stack = []

    if a == "." :
        break

    for i in a :
        if i == '[' or i == '(' :
            stack.append(i)
        elif i == ']' :
            if len(stack) != 0 and stack[-1] == '[' :
                stack.pop() # 맞으면 지워서 stack을 비워줌 0 = yes
            else : 
                stack.append(']')
                break
        elif i == ')' :
            if len(stack) != 0 and stack[-1] == '(' :
                stack.pop()
            else :
                stack.append(')')
                break
    if len(stack) == 0 :
        print('yes')
    else :
        print('no')

출처

profile
터벅터벅 개발(은좋은)자 로그

0개의 댓글

관련 채용 정보