[๋ฐฑ์ค-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 ๋ธ๋ก๊ทธ๋ฅผ ๋ณด๊ณ ์ฐธ์กฐํ๋๊ฒ ๋ซ๋ค..
'๐ Problem Solving > Baekjoon' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค-1748] ์ ์ด์ด ์ฐ๊ธฐ 1 / Python (0) | 2021.06.19 |
---|---|
[๋ฐฑ์ค-1292] ์ฝ๊ฒ ํธ๋ ๋ฌธ์ / Python (0) | 2021.06.19 |
[๋ฐฑ์ค-2563] ์์ข ์ด / Python (0) | 2021.06.19 |
[๋ฐฑ์ค-14503] ๋ก๋ด ์ฒญ์๊ธฐ / Python (0) | 2021.06.19 |
[๋ฐฑ์ค-2960] ์๋ผํ ์คํ ๋ค์ค์ ์ฒด / Python (0) | 2021.06.18 |