โ ์ ์ฒด์ฝ๋
import sys
from unicodedata import decimal
sys.stdin=open("C:\\\\input.txt",'rt')
a = input()
stack = []
res=''
print(a)
for x in a:
# ์
๋ ฅ๊ฐ x ๊ฐ ์ซ์๋ผ๋ฉด
if x.isdecimal():
res += x
# x๊ฐ ์ซ์๊ฐ ์๋๋ผ๋ฉด๋ฉด
else:
if x == '(':
stack.append(x)
elif x=='*' or x =='/':
while stack and (stack[-1] == '*' or stack[-1] =='/'):
res += stack.pop()
stack.append(x)
elif x =='+' or x=='-':
while stack and stack[-1] !='(':
res += stack.pop()
stack.append(x)
elif x == ')':
while stack and stack[-1] !='(':
res+=stack.pop()
stack.pop()
while stack:
res += stack.pop()
print(res)
๐ ์ค์ ํ๊ธฐ๋ฒ vs ํ์ ํ๊ธฐ๋ฒ
์ค์ ํ๊ธฐ๋ฒํ์ ํ๊ธฐ๋ฒ
3 + 5 | 3 5 + |
(3 + 5) * 2 | 3 5 + 2 * |
3 + 5 * 2 | 3 5 2 * + |
ํ์ ํ๊ธฐ๋ฒ์์๋ ๊ดํธ ์์ด๋ ์ฐ์ฐ ์ฐ์ ์์๋ฅผ ์ ํํ ํํ ๊ฐ๋ฅํด!
์ด ์ฝ๋๋ ์คํ์ ์ฌ์ฉํด ์ค์ ํ๊ธฐ๋ฒ์ ํ์ ํ๊ธฐ๋ฒ์ผ๋ก ๋ณํํ๋ ์๊ณ ๋ฆฌ์ฆ์ด์ผ.
๐ ์ฝ๋ ๋ถ์
import sys sys.stdin = open("C:\\Users\\csh\\Documents\\์ฝ๋ฉํ
์คํธ\\์๋ฃ๊ตฌ์กฐํ์ฉ(์คํ,ํ,ํด์ฌ,ํ)\\input.txt", 'rt') a = input() # ์ค์ ํ๊ธฐ๋ฒ ์์ ์
๋ ฅ stack = [] # ์ฐ์ฐ์๋ฅผ ์ ์ฅํ ์คํ res = '' # ๊ฒฐ๊ณผ ๋ฌธ์์ด (ํ์ ํ๊ธฐ๋ฒ ๋ณํ ๊ฒฐ๊ณผ)
- a: ์ค์ ํ๊ธฐ๋ฒ์ผ๋ก ๋ ์์ ์ ๋ ฅ
- stack: ์ฐ์ฐ์๋ฅผ ์ ์ฅํ๋ ์คํ
- res: ํ์ ํ๊ธฐ๋ฒ์ผ๋ก ๋ณํ๋ ๊ฒฐ๊ณผ
๐ข ์ซ์(0~9)์ธ ๊ฒฝ์ฐ → ๋ฐ๋ก ๊ฒฐ๊ณผ(res)์ ์ถ๊ฐ
if x.isdecimal(): res += x
- ์ซ์๋ ๋ฐ๋ก res์ ์ถ๊ฐ → ํ์ ํ๊ธฐ๋ฒ์์๋ ์ซ์์ ์์๋ ๊ทธ๋๋ก ์ ์ง๋จ.
๐ง ์ฐ์ฐ์(+, -, *, /, (, )) ์ฒ๋ฆฌ
1๏ธโฃ ( (์ด๋ฆฐ ๊ดํธ)
elif x == '(': stack.append(x)
- ์ด๋ฆฐ ๊ดํธ๋ ๋ฌด์กฐ๊ฑด ์คํ์ push
2๏ธโฃ *, / (๊ณฑ์ , ๋๋์ )
elif x == '*' or x == '/': while stack and (stack[-1] == '*' or stack[-1] == '/'): res += stack.pop() stack.append(x)
- *์ /๋ ๊ฐ์ ์ฐ์ ์์๋ผ๋ฆฌ ์ผ์ชฝ์์ ์ค๋ฅธ์ชฝ์ผ๋ก ์ฐ์ฐํด์ผ ํจ
- ์คํ์ top์ด * ๋๋ /์ด๋ฉด popํด์ res์ ์ถ๊ฐ
- ์ดํ ํ์ฌ ์ฐ์ฐ์๋ฅผ push
3๏ธโฃ +, - (๋ง์ , ๋บ์ )
elif x == '+' or x == '-': while stack and stack[-1] != '(': res += stack.pop() stack.append(x)
- +, -๋ *, /๋ณด๋ค ์ฐ์ ์์๊ฐ ๋ฎ์
- ์คํ์ ์ฐ์ฐ์๊ฐ ์์ผ๋ฉด popํ์ฌ res์ ์ถ๊ฐ
- ์ดํ ํ์ฌ ์ฐ์ฐ์๋ฅผ push
4๏ธโฃ ) (๋ซ๋ ๊ดํธ)
elif x == ')': while stack and stack[-1] != '(': res += stack.pop() stack.pop() # ์ฌ๋ ๊ดํธ '(' ์ ๊ฑฐ
- )๊ฐ ๋์ค๋ฉด ์ฌ๋ ๊ดํธ (๊ฐ ๋์ฌ ๋๊น์ง popํด์ res์ ์ถ๊ฐ
- (๋ ์คํ์์ ์ ๊ฑฐ๋ง ํ๊ณ res์ ์ถ๊ฐํ์ง ์์
๐ ๋ชจ๋ ๋ฌธ์๋ฅผ ํ์ธํ ํ, ์คํ์ ๋จ์ ์ฐ์ฐ์ ์ ๋ฆฌ
while stack: res += stack.pop()
- ์คํ์ ๋จ์ ์ฐ์ฐ์๋ค์ ์ฐจ๋ก๋ก res์ ์ถ๊ฐ
- ์ด์ ํ์ ํ๊ธฐ๋ฒ์ผ๋ก ๋ณํ ์๋ฃ!
๐ก ์คํ ์์
์ ๋ ฅ:
3+(5*2)-8
๋ณํ ๊ณผ์
๋ฌธ์์ฐ์ฐ์คํ ์ํํ์ ํ๊ธฐ๋ฒ (res)
3 | ์ซ์ → res ์ถ๊ฐ | [] | 3 |
+ | ์ฐ์ฐ์ → stack ์ถ๊ฐ | ['+'] | 3 |
( | ์ฐ์ฐ์ → stack ์ถ๊ฐ | ['+', '('] | 3 |
5 | ์ซ์ → res ์ถ๊ฐ | ['+', '('] | 3 5 |
* | ์ฐ์ฐ์ → stack ์ถ๊ฐ | ['+', '(', '*'] | 3 5 |
2 | ์ซ์ → res ์ถ๊ฐ | ['+', '(', '*'] | 3 5 2 |
) | (๊น์ง pop → res ์ถ๊ฐ | ['+'] | 3 5 2 * |
- | + pop → res ์ถ๊ฐ, - push | ['-'] | 3 5 2 * + |
8 | ์ซ์ → res ์ถ๊ฐ | ['-'] | 3 5 2 * + 8 |
EOF | ์คํ pop → res ์ถ๊ฐ | [] | 3 5 2 * + 8 - |
์ถ๋ ฅ ๊ฒฐ๊ณผ:
3 5 2 * + 8 -
โณ ์๊ฐ ๋ณต์ก๋ ๋ถ์
- O(n): ํ ๋ฒ์ ๋ฌธ์์ด ์ํ(for) + ์ฐ์ฐ์ ์ฒ๋ฆฌ(while)
- ์คํ ์ฌ์ฉ์ผ๋ก ์ฐ์ฐ์์ ์ฐ์ ์์๋ฅผ ํจ๊ณผ์ ์ผ๋ก ๊ด๋ฆฌ!
๐ ์ ๋ฆฌ
โ
์ค์ ํ๊ธฐ๋ฒ์ ํ์ ํ๊ธฐ๋ฒ์ผ๋ก ๋ณํํ๋ ์๊ณ ๋ฆฌ์ฆ
โ
์คํ์ ์ฌ์ฉํด ์ฐ์ฐ์ ์ฐ์ ์์ ๊ด๋ฆฌ
โ
**์๊ฐ ๋ณต์ก๋ O(n)**์ผ๋ก ํจ์จ์ ์ธ ์ฐ์ฐ
์ด์ ํ์ ํ๊ธฐ๋ฒ์ผ๋ก ๋ณํํ ์์์ ์คํ์ ์ด์ฉํด ๊ณ์ฐํ ์๋ ์์ด!
์ดํด ์ ๋๋ ๋ถ๋ถ ์์ผ๋ฉด ์ง๋ฌธํด์ค ๐๐ฅ
'์๊ณ ๋ฆฌ์ฆ ๐ก' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
2. ์ ๋ง๋๊ธฐ(์คํ)_python (0) | 2025.02.13 |
---|---|
1. ๊ฐ์ฅ ํฐ ์(์คํ)_python (0) | 2025.02.13 |
๋๋ช ๋๋ฌผ ์ ์ฐพ๊ธฐ (0) | 2024.11.10 |
์ฝ๋ฉํ ์คํธ ์ถ์ ๊ฒฝํฅ/ ์ฑ์ ๊ธฐ์ค (0) | 2023.10.09 |
[๋ฐฑ์ค 2562] ์ต๋๊ฐ.java (1) | 2023.01.11 |