Post

[BOJ] 9012. ๊ด„ํ˜ธ(python)

๐Ÿ“Œ๋ฌธ์ œ

Alt text

๐Ÿ’ช์•„์ด๋””์–ด

  1. โ€™( )โ€™ ์ง ๋งž์ถ”๊ธฐ
    • โ€™(โ€˜๋ฉด ์Šคํƒ์— ๋„ฃ๊ธฐ
    • โ€™)โ€™๋ฉด ์Šคํƒ[-1] โ€˜(โ€˜ ๋นผ๊ธฐ
      stack์ด ๋น„์–ด์žˆ๋Š” ๋ฐ โ€˜)โ€™๋“ค์–ด์˜ค๋ฉด VPS์•„๋‹˜
    • ๋ฌธ์ž์—ด์ด ๋๋‚ฌ๋Š”๋ฐ ๋นˆ stack์ด ์•„๋‹ˆ๋ฉด VPS์•„๋‹˜


๐Ÿฅ‚์ฝ”๋“œ

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
T = int(input())
for _ in range(T):
    PS = input()
    left_stack=[]
    answer='NO'
    for ch in PS:
        if ch=='(':left_stack.append(ch)
        else:
            if not left_stack: break
            left_stack.pop()
    else: 
        if not left_stack: answer='YES'

    print(answer) 

This post is licensed under CC BY 4.0 by the author.

Comments powered by Disqus.