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()
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')
출처