BOJ4949 균형잡힌 세상

randi65535·2020년 12월 6일
0

스택이 차있을 때를 생각 못해서 오래 잡고 있었던 문제

ans = []
while 1:
	txt = input()
	if len(txt) == 1 and txt[0] == '.':
		print('\n'.join(ans))
		break
	
	st = []
	yesno = 'yes'
	
	for i in range(len(txt)):
		# '([' 를 만난 경우
		if txt[i] in '([': 
			st.append(txt[i])
			continue
		
		#스택이 비어있지 않을 때
		if st:
			# 짝맞추기
			if st[-1] == '(':
				if txt[i] == ')':
					st.pop()
				if txt[i] == ']':
					yesno = 'no'
					break

			# 짝맞추기
			elif st[-1] == '[':
				if txt[i] == ']':
					st.pop()
				if txt[i] == ')':
					yesno = 'no'
					break
				
		#스택이 비어있을 때
		else:
			# 스택이 비어있는데 ')' or ']'
			if txt[i] in ')]':
				yesno = 'no'
				break

	if st:
		yesno = 'no'

	ans.append(yesno)
profile
unsinged int 8byte-1

0개의 댓글