Algorithm Problem/Python
[python] ๋ฐฑ์ค - 2504. ๊ดํธ์ ๊ฐ
deo2kim
2020. 10. 13. 20:03
๋ฐ์ํ
๐ค๋ฌธ์ ํด๊ฒฐ
-
S2 | ์คํ, ์๋ฃ๊ตฌ์กฐ,
๊ตฌํ, ์ฌ๊ท
๊ดํธ๋ฌธ์ ๋ ํญ์ ์คํ์ผ๋ก ํ์๋ค.
- ๋จผ์ ์ฌ๋ฐ๋ฅธ ๊ดํธ์ธ์ง ์ฒดํฌํ์! ๊ทธ ๋ค์
- ์คํ์ ์ด์ฉํด ๊ดํธ ์ง์ ๋ง์ถ๋ฉด์ ()์ผ ๋ 2, []์ผ ๋ 3์ ๊ณ์ฐํด์ค๋ค.
- ๋ง์ฝ ์คํ์ ๋ง์ง๋ง์ ์ซ์๊ฐ ์๋ค๋ฉด ๊ณฑํด์ค๋ค.
- ๋ง์ฝ ์คํ์ ์ซ์๊ฐ ์ฐ์์ผ๋ก ๋์ด๋๋ฉด ๋ํด์ค๋ค.
๐ป์์ค ์ฝ๋
def is_right(s):
stack = []
for ss in s:
if ss == '(':
stack.append(ss)
elif ss == ')':
if stack and stack[-1] == '(':
stack.pop()
else:
return False
elif ss == '[':
stack.append(ss)
elif ss == ']':
if stack and stack[-1] == '[':
stack.pop()
else:
return False
if stack:
return False
return True
def my_stack(s):
stack = []
for c in s:
print(stack)
if c == '(':
stack.append(c)
elif c == '[':
stack.append(c)
elif c == ')':
tmp = 2
while True:
t = stack.pop()
if type(t) == int:
tmp *= t
elif t == '(':
stack.append(tmp)
break
elif c == ']':
tmp = 3
while True:
t = stack.pop()
if type(t) == int:
tmp *= t
elif t == '[':
stack.append(tmp)
break
tmp = 0
while stack:
t = stack.pop()
if type(t) == int:
tmp += t
else:
stack.append(t)
break
if tmp:
stack.append(tmp)
return stack[-1]
S = input()
if is_right(S):
print(my_stack(S))
else:
print(0)
๐๋ฌธ์ ํ์ธ
์ถ์ฒ: BACKJOON ONLINE JUDGE
๋งํฌ: https://www.acmicpc.net/problem/2504
2504๋ฒ: ๊ดํธ์ ๊ฐ
4๊ฐ์ ๊ธฐํธ ‘(’, ‘)’, ‘[’, ‘]’๋ฅผ ์ด์ฉํด์ ๋ง๋ค์ด์ง๋ ๊ดํธ์ด ์ค์์ ์ฌ๋ฐ๋ฅธ ๊ดํธ์ด์ด๋ ๋ค์๊ณผ ๊ฐ์ด ์ ์๋๋ค. ํ ์์ ๊ดํธ๋ก๋ง ์ด๋ฃจ์ด์ง ‘()’์ ‘[]’๋ ์ฌ๋ฐ๋ฅธ ๊ดํธ์ด์ด๋ค. ๋ง์ผ
www.acmicpc.net
๋ฐ์ํ