본문 바로가기

코딩테스트

[백준] 9012번 : 막대 - 파이썬

아이디어 : 스택

문제 해결 과정

파이썬은 리스트로 스택을 사용할 수 있음

스택 : stack = []

푸쉬 : stack.append(x)

팝 : stack.pop()

 

나의 답안

# -*- coding: utf-8 -*-
import sys

input = sys.stdin.readline
t = int(input().strip())

while t:
    t = t - 1
    bars = input().strip()
    stack = []
    invalid = False

    for elem in bars:
        if elem == "(":
            stack.append(elem)
        else:
            if len(stack) == 0:
                invalid = True
                continue
            else:
                stack.pop()

    if invalid or len(stack) != 0:
        print("NO")
    else:
        print("YES")

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

 

9012번: 괄호

괄호 문자열(Parenthesis String, PS)은 두 개의 괄호 기호인 ‘(’ 와 ‘)’ 만으로 구성되어 있는 문자열이다. 그 중에서 괄호의 모양이 바르게 구성된 문자열을 올바른 괄호 문자열(Valid PS, VPS)이라고

www.acmicpc.net