๊ธฐ๋ก๋ฐฉ

BOJ_4949 : ๊ท ํ˜•์žกํžŒ ์„ธ์ƒ ๋ณธ๋ฌธ

CodingTest/Python

BOJ_4949 : ๊ท ํ˜•์žกํžŒ ์„ธ์ƒ

Soom_1n 2022. 8. 8. 19:27

๐Ÿ‘‰ ๋ฌธ์ œ๋งํฌ

 

4949๋ฒˆ: ๊ท ํ˜•์žกํžŒ ์„ธ์ƒ

ํ•˜๋‚˜ ๋˜๋Š” ์—ฌ๋Ÿฌ์ค„์— ๊ฑธ์ณ์„œ ๋ฌธ์ž์—ด์ด ์ฃผ์–ด์ง„๋‹ค. ๊ฐ ๋ฌธ์ž์—ด์€ ์˜๋ฌธ ์•ŒํŒŒ๋ฒณ, ๊ณต๋ฐฑ, ์†Œ๊ด„ํ˜ธ("( )") ๋Œ€๊ด„ํ˜ธ("[ ]")๋“ฑ์œผ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์œผ๋ฉฐ, ๊ธธ์ด๋Š” 100๊ธ€์ž๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™๋‹ค. ๊ฐ ์ค„์€ ๋งˆ์นจํ‘œ(".")๋กœ ๋๋‚œ๋‹ค

www.acmicpc.net



๐Ÿ”ธ ๋ฌธ์ œ ๋ถ„์„ ๐Ÿ”ธ

  • ๋ฌธ์ž์—ด์„ ์ฒ˜๋ฆฌ์™€ ์Šคํƒ ํ™œ์šฉ์ด ํ•ฉ์ณ์ ธ์žˆ๋Š” ๋ฌธ์ œ์ด๋‹ค.

๐Ÿ”ธ ์ฝ”๋“œ ๐Ÿ”ธ

import sys

input = sys.stdin.readline

while(True):
    ss = input().rstrip()
    if ss == ".": break

    stack = []
    check = False
    for c in ss:
        if c == '(' or c == '[':
            stack.append(c)
        elif c == ')':
            if len(stack) == 0 or stack[-1] != '(': check = True
            else: stack.pop(-1)
        elif c == ']':
            if len(stack) == 0 or stack[-1] != '[': check = True
            else: stack.pop(-1)
    
    if check: print("no")
    else:
        if len(stack) != 0: print("no")
        else: print("yes")

๐Ÿ”ธ ์ฝ”๋“œ ํ•ด์„ ๐Ÿ”ธ

  • ํ•œ ์ค„ ์ž…๋ ฅ์ด ๊ธด ๊ฒƒ ๊ฐ™์•„ ๋น ๋ฅธ ์ž…๋ ฅ์„ ์‚ฌ์šฉํ–ˆ๋‹ค.
  • stack ๋ฆฌ์ŠคํŠธ๋ฅผ ์Šคํƒ์œผ๋กœ ํ™œ์šฉํ–ˆ๋‹ค.
    • ๊ด„ํ˜ธ์˜ ์Œ์ด ๋งž์œผ๋ฉด ์Œ์„ ์Šคํƒ์—์„œ ์ œ๊ฑฐํ•œ๋‹ค.
  • ์Œ์ด ๋งž์ง€ ์•Š๋Š” ๋ฌธ์ž์—ด์ด๋ฉด check๋ฅผ True๋กœ ๋ฐ”๊ฟ”์ค€๋‹ค.

๐Ÿ”ธ end ๐Ÿ”ธ

  • ์ง€๊ธˆ๋ณด๋‹ˆ check๊ฐ€ True๊ฐ€ ๋  ๋•Œ break๋ฅผ ๋„ฃ์–ด์ค˜์•ผ ํ•˜๋Š”๋ฐ ๊ทธ๋ƒฅ ์ง„ํ–‰ํ•ด๋„ ํ†ต๊ณผ๋๋‹ค.
  • ์ฒซ ์‹œ๋„์—์„œ ์˜๋„์น˜์•Š๊ฒŒ ์˜คํƒ€๋กœ ์ธํ•œ from turtle import st ์ด๋ผ๋Š” ๋ชจ๋“ˆ์ด ๋“ค์–ด๊ฐ€์„œ ์ฑ„์ ์„ ์‹คํŒจํ–ˆ๋‹ค.
    • ์˜คํƒ€์— ์ฃผ์˜ํ•ด์•ผ๊ฒ ๋‹ค. vscode์˜ ํŽธ์˜์„ฑ์ด ๋ฐฉํ•ด๊ฐ€ ๋œ ๋ถ€๋ถ„์ด๋‹ค...

728x90

'CodingTest > Python' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

BOJ_10816 : ์ˆซ์ž ์นด๋“œ 2  (0) 2022.08.10
BOJ_10773 : ์ œ๋กœ  (0) 2022.08.09
BOJ_11651 : ์ขŒํ‘œ ์ •๋ ฌํ•˜๊ธฐ 2  (0) 2022.08.07
BOJ_7568 : ๋ฉ์น˜  (0) 2022.08.07
BOJ_9012 : ๊ด„ํ˜ธ  (0) 2022.08.07