#25 백준 9012번 파이썬
우보만리
- stack 구조를 활용하는 문제
(
는 stack에 넣고,)
가 입력될 때 하나씩 pop하는 방식으로 구현하고, YES, No 판단은 stack의 사이즈로 했다.- 문제 유형에 stack이라는 힌트를 보고 다음과 같이 풀이했지만, 이 방법이 최선은 아닐 것 같다는 생각이 든다.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def check_vps(ps): | |
stack = [] | |
if ps[0] == ')' or ps[-1] == '(': | |
print('NO') | |
return | |
for c in ps: | |
if c == '(': | |
stack.append(c) | |
else: | |
if len(stack) == 0: | |
print('NO') | |
return | |
else: | |
stack.pop() | |
if len(stack) == 0: | |
print('YES') | |
else : | |
print('NO') | |
return | |
T = int(input()) | |
for _ in range(T): | |
ps = input() | |
check_vps(ps) | |