์กฐ๊ฑด์ ์์ฒญ ์ถ๊ฐํด์ ํผ ๊ตฌํ ๋ฌธ์ . ์ฝ๋๊ฐ ๋๋ฌด ๋๋ฌ์ด๋ฐ ๋ ์ข์ ํ์ด ๋ฐฉ๋ฒ์ด ์์ ๋ฏ
๐ผ ๋ฌธ์ ๋งํฌ
https://www.acmicpc.net/problem/2504
- ํฐ์ด: ์ค๋ฒ I
- ๋ถ๋ฅ: ๊ตฌํ, ์๋ฃ๊ตฌ์กฐ, ์คํ, ์ฌ๊ท
โ TRIAL 1.
๋๋ณด๊ธฐ
์ฝ๋
inputs = list(input())
couple = {'(': ')', '[': ']'}
value = {'(': 2, '[': 3}
while len(inputs) != 1:
ctr = 0
new_inputs = []
while ctr < len(inputs)-1:
v = inputs[ctr]
w = inputs[ctr+1]
if v == '(' or v == '[':
if w == couple[v]: #() or []: change into number
ctr += 2
new_inputs.append(value[v])
elif type(w) == int: #(w) or {w}: change into 2/3*w
x = inputs[ctr+2]
if x == couple[v]:
ctr += 3
new_inputs.append(value[v]*w)
else:
ctr += 1
new_inputs.append(v)
else:
ctr += 1
new_inputs.append(v)
elif type(v) == int:
if type(w) == int:
ctr += 2
new_inputs.append(v+w)
else:
ctr += 1
new_inputs.append(v)
else:
ctr += 1
new_inputs.append(v)
if ctr == len(inputs)-1:
new_inputs.append(inputs[ctr])
if len(inputs) == len(new_inputs):
inputs = [0]
else:
inputs = new_inputs
print(inputs[0])
ํ๋ฆฐ ์์ธ
๋ฐํ์ ์๋ฌ (IndexError). ๋งค๋ฒ ํ์ฌ node์ ๋ค์ node๋ง ๋น๊ตํ๋ค๊ฐ (X) ํํ์ผ ๊ฒฝ์ฐ๋ฅผ ์ฐพ๊ธฐ ์ํด ๋ค๋ค์ node๊น์ง ๊ฒ์ฌํ๋ ๊ฒฝ์ฐ๊ฐ ์์๋๋ฐ, ์ด ๋ (X์์ ๊ฐ์ด ๋๋๋ ๊ฒฝ์ฐ์ index error๊ฐ ๋ฐ์.
์์ธ ์ผ์ด์ค: https://www.acmicpc.net/board/view/68189
๐ก Solution
์ฝ๋
inputs = list(input())
couple = {'(': ')', '[': ']'}
value = {'(': 2, '[': 3}
while len(inputs) > 1 or type(inputs[0]) != int:
ctr = 0
new_inputs = []
while ctr < len(inputs)-1:
v = inputs[ctr]
w = inputs[ctr+1]
if v == '(' or v == '[':
if w == couple[v]: #() or []: change into number
ctr += 2
new_inputs.append(value[v])
elif type(w) == int: #(w) or {w}: change into 2/3*w
try:
x = inputs[ctr+2]
except:
inputs = [0]
break
if x == couple[v]:
ctr += 3
new_inputs.append(value[v]*w)
else:
ctr += 1
new_inputs.append(v)
else:
ctr += 1
new_inputs.append(v)
elif type(v) == int:
if type(w) == int:
ctr += 2
new_inputs.append(v+w)
else:
ctr += 1
new_inputs.append(v)
else:
ctr += 1
new_inputs.append(v)
if ctr == len(inputs)-1:
new_inputs.append(inputs[ctr])
if len(inputs) == len(new_inputs):
inputs = [0]
else:
inputs = new_inputs
print(inputs[0])
์ฝ๋ ์ค๋ช
- ์ฌ๊ท์ ์ผ๋ก ๊ฐ๋จํ ๊ฒฝ์ฐ๋ถํฐ ์ซ์๋ก ๋ฐ๊ฟ ์ค.
- () : ์ซ์๋ก ๋ณ๊ฒฝ
- (X): X*2๋ก ๋ณ๊ฒฝ. (์ด ๊ฒฝ์ฐ์ ๋ง์ฝ IndexError๊ฐ ๋ฐ์ํ๋ฉด, ๋ง์ง๋ง ๊ดํธ๊ฐ ๋์ค๊ธฐ ์ ์ ๋ฌธ์์ด์ด ๋๋ ๊ฒ์ด๋ฏ๋ก ๋ฌธ์์ด์ ์ค๋ฅ๊ฐ ์๋ ๊ฒ. ๋ฐ๋ก 0 ๋ฐํ.)
- XY: X+Y๋ก ๋ณ๊ฒฝ
- [์ ๋ํด์๋ ๋์ผํ๊ฒ ์ ์ฉ
- ๋ง์ฝ ๋๊น์ง ๋ค ๋์๋๋ฐ๋ ๊ธธ์ด๊ฐ ๋ฐ๋์ง ์์๋ค๋ฉด ๊ท์น์ ๋ถํฉํ๋ ๊ฒฝ์ฐ๊ฐ ์๋ ๊ฒฝ์ฐ. ์ด ๊ฒฝ์ฐ ๋ฌธ์์ด์ ์ค๋ฅ๊ฐ ์๋ ๊ฒ์ด๋ฏ๋ก ๋ฐ๋ก 0 ๋ฐํ.
- ๋ฌธ์์ด์ด ์ ์ ํ๋๋ง ๋จ์ ๋๊น์ง ๋ฐ๋ณต.
๋ฐ์ํ
'๐ CS > ์ฝ๋ฉํ ์คํธ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค 14500๋ฒ] ํ ํธ๋ก๋ฏธ๋ ธ (python/ํ์ด์ฌ) (2) | 2022.09.14 |
---|---|
[๋ฐฑ์ค 7569๋ฒ] ํ ๋งํ (python/ํ์ด์ฌ) (0) | 2022.09.09 |
[๋ฐฑ์ค 16926๋ฒ] ๋ฐฐ์ด ๋๋ฆฌ๊ธฐ 1 (python/ํ์ด์ฌ) (0) | 2022.07.05 |
[๋ฐฑ์ค 1697๋ฒ] ์จ๋ฐ๊ผญ์ง (python/ํ์ด์ฌ) (0) | 2022.06.30 |
[๋ฐฑ์ค 1012๋ฒ] ์ ๊ธฐ๋ ๋ฐฐ์ถ (python/ํ์ด์ฌ) (0) | 2022.06.29 |