๋ฐ์ํ
๐ค๋ฌธ์ ํด๊ฒฐ
-
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
๋ฐ์ํ
'Algorithm Problem > Python' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[python] ๋ฐฑ์ค - 1735. ๋ถ์ ํฉ (0) | 2020.10.15 |
---|---|
[python] ๋ฐฑ์ค - 1965. ์์ ๋ฃ๊ธฐ (0) | 2020.10.14 |
[python] ๋ฐฑ์ค - 1890. ์ ํ (0) | 2020.10.12 |
[python] ๋ฐฑ์ค - 1780. ์ข ์ด์ ๊ฐ์ (0) | 2020.10.11 |
[python] ๋ฐฑ์ค - 11279. ์ต๋ ํ (0) | 2020.10.10 |