๐ŸŒฑ → ๐ŸŒณ

[๋ฐฑ์ค€] 9012๋ฒˆ: ๊ด„ํ˜ธ python ๋ณธ๋ฌธ

Algorithms

[๋ฐฑ์ค€] 9012๋ฒˆ: ๊ด„ํ˜ธ python

BAY 2023. 1. 3. 11:41
728x90

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

 

9012๋ฒˆ: ๊ด„ํ˜ธ

๊ด„ํ˜ธ ๋ฌธ์ž์—ด(Parenthesis String, PS)์€ ๋‘ ๊ฐœ์˜ ๊ด„ํ˜ธ ๊ธฐํ˜ธ์ธ ‘(’ ์™€ ‘)’ ๋งŒ์œผ๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ๋Š” ๋ฌธ์ž์—ด์ด๋‹ค. ๊ทธ ์ค‘์—์„œ ๊ด„ํ˜ธ์˜ ๋ชจ์–‘์ด ๋ฐ”๋ฅด๊ฒŒ ๊ตฌ์„ฑ๋œ ๋ฌธ์ž์—ด์„ ์˜ฌ๋ฐ”๋ฅธ ๊ด„ํ˜ธ ๋ฌธ์ž์—ด(Valid PS, VPS)์ด๋ผ๊ณ 

www.acmicpc.net

 

์„ค๊ณ„ ๋ฐฉ๋ฒ•

๋‚˜์˜ ๋ฐฉ๋ฒ•

  • ps๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ “(”๋ฉด stack์— ๋„ฃ๊ณ  “)”๋ฉด stack์—์„œ ๋นผ์ฃผ์—ˆ์Œ
  • “)”์ธ๋ฐ stack์—์„œ ๋บ„ ๊ฒŒ ์—†์œผ๋ฉด vps๊ฐ€ ์•„๋‹Œ๊ฑฐ๊ณ , ps๋ฅผ ๋ชจ๋‘ ๋Œ๊ณ  ๋‚˜์„œ๋„ (๊ฐ€ ๋‚จ์•„์žˆ์–ด๋„ vps๊ฐ€ ์•„๋‹˜

๋‹ค๋ฅธ ๋ฐฉ๋ฒ•

  • ๊ผญ stack์„ ์ด์šฉํ•˜์ง€ ์•Š๋”๋ผ๋„ sum = 0 ์ด๋ผ๊ณ  ์„ ์–ธ ํ•˜์—ฌ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์Œ
  • “(”๋ฉด sum +=1, “)” ์ด๋ฉด sum -=1 ์ด๋Ÿฐ ์‹์œผ๋กœ

 

์ฝ”๋“œ

# ๋‚ด ์ฝ”๋“œ 
# ์Šคํƒ ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ์ด์šฉํ•œ ํ’€์ด 
N = int(input())

for _ in range(N):
  ps = list(input())
  stack = []
  ans = ''
  for i in ps:
    if i == "(":
      stack.append(i)
    elif i == ")":
      if len(stack) == 0:
        # print("NO")
        ans = 'no'
        break
      else:
        stack.pop()
  if len(stack) == 0 and ans != 'no':
    print('YES')
  else:
    print('NO')
# sum์„ ์ด์šฉํ•œ ํ’€์ด 
a = int(input())

for i in range(a):
    b = input()
    s = list(b)
    sum = 0
    for i in s:
        if i == '(':
            sum += 1
        elif i == ')':
            sum -= 1
        if sum < 0:
            print('NO')
            break
    if sum > 0:
        print('NO')
    elif sum == 0:
        print('YES')

 

์‹œ๊ฐ„ ๋ณต์žก๋„

$O(n^2)$

 

์–ด๋ ค์› ๋˜ ์ 

  • “)”์ธ๋ฐ stack์—์„œ ๋บ„ ๊ฒŒ ์—†์œผ๋ฉด break๋ฅผ ํ•˜๊ณ  stack์˜ ๊ธธ์ด๋กœ ‘ps๋ฅผ ๋ชจ๋‘ ๋Œ๊ณ  ๋‚˜์„œ๋„ (๊ฐ€ ๋‚จ์•„์žˆ์–ด๋„ vps๊ฐ€ ์•„๋‹˜’ ์˜ ๊ฒฝ์šฐ๋ฅผ ์ž‘์„ฑํ•˜๋ ค๊ณ  ํ•˜๋‹ˆ print๊ฐ€ 2๋ฒˆ ์ถœ๋ ฅ๋˜๋Š” ํ˜„์ƒ์ด ๋‚˜ํƒ€๋‚ฌ๋‹ค
  • ์ผ๋‹จ ๋‚˜๋Š” ans๋ผ๋Š” ๋ณ€์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด์„œ ans๊ฐ€ ๋นˆ ๋ฐฐ์—ด์ธ ๊ฒฝ์šฐ์—๋งŒ ์•„๋ž˜์˜ if๋ฌธ์ด ์‹คํ–‰๋˜๋„๋ก ํ•˜์˜€๋Š”๋ฐ ์•„๋ž˜์˜ ์˜ˆ์‹œ๋ฅผ ๋ณด๋‹ˆ for j ์™€ ๊ฐ™์€ ๋ผ์ธ์— else๋ฅผ ํ•˜๋‹ˆ break๋ฌธ์œผ๋กœ ๋Š์–ด์ง€์ง€ ์•Š๊ณ  ์ˆ˜ํ–‰๋˜์—ˆ์„ ๊ฒฝ์šฐ ์ˆ˜ํ–‰ํ•˜๋„๋ก ํ•  ์ˆ˜ ์žˆ์€ ๊ฒƒ์„ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค
  • break์— ๋Œ€ํ•ด ์ข€ ๋” ์•Œ์•„๋ด์•ผ ํ•  ๊ฒƒ ๊ฐ™๋‹ค
T = int(input())

for i in range(T):
    stack = []
    a=input()
    for j in a:
        if j == '(':
            stack.append(j)
        elif j == ')':
            if stack:
                stack.pop()
            else: # ์Šคํƒ์— ๊ด„ํ˜ธ๊ฐ€ ์—†์„๊ฒฝ์šฐ NO
                print("NO")
                break
    else: # break๋ฌธ์œผ๋กœ ๋Š๊ธฐ์ง€ ์•Š๊ณ  ์ˆ˜ํ–‰๋ฌ์„๊ฒฝ์šฐ ์ˆ˜ํ–‰ํ•œ๋‹ค
        if not stack: # break๋ฌธ์œผ๋กœ ์•ˆ๋Š๊ธฐ๊ณ  ์Šคํƒ์ด ๋น„์–ด์žˆ๋‹ค๋ฉด ๊ด„ํ˜ธ๊ฐ€ ๋‹ค ๋งž๋Š”๊ฑฐ๋‹ค.
            print("YES")
        else: # break์•ˆ ๊ฑธ๋ ธ๋”๋ผ๋„ ์Šคํƒ์— ๊ด„ํ˜ธ๊ฐ€ ๋“ค์–ด์žˆ๋‹ค๋ฉด NO์ด๋‹ค.
            print("NO")

 

์ฐธ๊ณ  ์ž๋ฃŒ

https://pacific-ocean.tistory.com/70

https://wookcode.tistory.com/49

728x90