본문 바로가기
백준/실버

백준9012. 괄호 - Python, stack

by 아찌방 2025. 5. 10.

 

출처 : https://www.acmicpc.net/problem/9012

 

 

 

 

 

코드 & 풀이

 

import sys

# 입력 처리
data = sys.stdin.read().splitlines()
N = int(data.pop(0))

def VPS(pstring):
    stack = []
    for parenthesis in pstring:
        if parenthesis == ')':
            if not stack:
                return "NO"
            stack.pop()
        else:
            stack.append(parenthesis)

    return "NO" if stack else "YES"

answer = ""
for _ in range(N):
    answer += VPS(data.pop(0)) + "\n"

print(answer)

 

 

스택을 사용하는데

 

스택에는 ( 만 넣을 겁니다.

 

그렇기에 ) 가 오면 무조건 pop 하는거죠.

 

그런데

 

stack이 비어있다?

 

그러면 VPS가 안된다는 것이기 때문에

 

NO를 반환해줍니다.

 

 

 

 

 

다음에 또 봐요

 

728x90