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")