[๋ฐฑ์ค€-2504] ๊ด„ํ˜ธ์˜ ๊ฐ’ / Python

๐Ÿ“š Problem Solving/Baekjoon

 

2504๋ฒˆ: ๊ด„ํ˜ธ์˜ ๊ฐ’

4๊ฐœ์˜ ๊ธฐํ˜ธ ‘(’, ‘)’, ‘[’, ‘]’๋ฅผ ์ด์šฉํ•ด์„œ ๋งŒ๋“ค์–ด์ง€๋Š” ๊ด„ํ˜ธ์—ด ์ค‘์—์„œ ์˜ฌ๋ฐ”๋ฅธ ๊ด„ํ˜ธ์—ด์ด๋ž€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ •์˜๋œ๋‹ค. ํ•œ ์Œ์˜ ๊ด„ํ˜ธ๋กœ๋งŒ ์ด๋ฃจ์–ด์ง„ ‘()’์™€ ‘[]’๋Š” ์˜ฌ๋ฐ”๋ฅธ ๊ด„ํ˜ธ์—ด์ด๋‹ค.  ๋งŒ์ผ

www.acmicpc.net

import sys

input = sys.stdin.readline

s = input().rstrip()
stack = []
answer = 0

for i in s:
    if i == ")":
        t = 0
        while len(stack) != 0:
            top = stack.pop()
            if top == "(":
                if t == 0:
                    stack.append(2)
                else:
                    stack.append(2 * t)
                break
            elif top == "[":
                print(0)
                exit(0)
            else:
                t = t + int(top)
    elif i == "]":
        t = 0
        while len(stack) != 0:
            top = stack.pop()
            if top == "[":
                if t == 0:
                    stack.append(3)
                else:
                    stack.append(3 * t)
                break
            elif top == "(":
                print(0)
                exit(0)
            else:
                t = t + int(top)
    else:
        stack.append(i)

for i in stack:
    if i == "(" or i == "[":
        print(0)
        exit(0)
    else:
        answer += i

print(answer)

 

ํ•ด์„ค

ํ’€๋‹ค๊ฐ€ ํ•ด๊ฒฐ์„ ๋ชปํ•ด์„œ ๊ฒฐ๊ตญ ๊ตฌ๊ธ€๋ง์„ ํ–ˆ๋‹ค.. ์ •ํ™•ํ•œ ํ•ด์„ค๊ณผ ๋‹ต์•ˆ ์ฝ”๋“œ๋Š” IT-garden ๋ธ”๋กœ๊ทธ๋ฅผ ๋ณด๊ณ  ์ฐธ์กฐํ•˜๋Š”๊ฒŒ ๋‚ซ๋‹ค..