๐งฎ PS
๋ฐฑ์ค (boj) 10828 ํ์ด์ฌ - ์คํ
Newmon
2020. 3. 20. 17:00
๋ฌธ์ ๋งํฌ : https://www.acmicpc.net/problem/10828
10828๋ฒ: ์คํ
์ฒซ์งธ ์ค์ ์ฃผ์ด์ง๋ ๋ช ๋ น์ ์ N (1 ≤ N ≤ 10,000)์ด ์ฃผ์ด์ง๋ค. ๋์งธ ์ค๋ถํฐ N๊ฐ์ ์ค์๋ ๋ช ๋ น์ด ํ๋์ฉ ์ฃผ์ด์ง๋ค. ์ฃผ์ด์ง๋ ์ ์๋ 1๋ณด๋ค ํฌ๊ฑฐ๋ ๊ฐ๊ณ , 100,000๋ณด๋ค ์๊ฑฐ๋ ๊ฐ๋ค. ๋ฌธ์ ์ ๋์์์ง ์์ ๋ช ๋ น์ด ์ฃผ์ด์ง๋ ๊ฒฝ์ฐ๋ ์๋ค.
www.acmicpc.net
๋ฑํ ์ด๋ ค์ ๋๊ฑด ์์์ง๋ง sys.readline.rstrip() ์ ์ฌ์ฉํ์ง ์๊ณ ๊ทธ๋ฅ input()์ผ๋ก ๋ฐ์๋๋ ์๊ฐ์ด๊ณผ๊ฐ ๋ฐ์ํ์๋ค.
import sys
input = sys.stdin.readline
N = int(input())
stack = []
for i in range(N):
command = input().rstrip()
if " " in command : #์
๋ ฅ ๋ฌธ์์ด์ ๊ณต๋ฐฑ์ด ํฌํจ๋๊ฑด push๋ฐ์ ์๋ค.
c,n = command.split()
stack.append(int(n))
elif command == 'pop' :
if len(stack) > 0 : print(stack.pop(-1))
else : print(-1)
elif command == 'size' : print(len(stack))
elif command == 'empty' : print(1 if len(stack)==0 else 0)
elif command == 'top' :
if len(stack) > 0 : print(stack[-1])
else : print(-1)