백준 / 균형잡힌 세상 / 4949

박성완·2022년 4월 27일
0

백준

목록 보기
77/78
post-thumbnail

Question

문제링크
Silver 4

Logic

기본 구조 : loop
1. 입력 한 줄씩 읽어서 조건에 비교한다.
2. 열린괄호를 만나면 stack에 추가하고, 닫힌 괄호를 만나면 stack에서 하나를 제거한다. 이 때 서로 맞지 않는 기호이거나, 스택이 비어있다면 조건을 불충족한다.
3. 조건을 중촉하고 stack이 비어있다면 'yes'를 출력한다.

Code

from sys import stdin

while True :
    sen = stdin.readline().rstrip()
    if sen == '.' : break
    stack = []
    ans = True
    for s in sen:
        if s=='(' or s=='[' : stack.append(s)
        elif s==')' :
            if len(stack)==0 or stack[-1] != '(':
                ans = False
                break
            else : stack.pop()
        elif s==']' :
            if len(stack)==0 or stack[-1] != '[':
                ans = False
                break
            else : stack.pop()
    print("yes") if (ans and not stack) else print("no")

0개의 댓글