방법1. stack을 사용해서 주어진 조건대로 구현하면 된다.
import sys
n = int(sys.stdin.readline())
stack = []
for _ in range(n):
w = sys.stdin.readline().rstrip().split()
if w[0]=='push':
stack.append(w[1])
elif w[0]=='pop':
if stack:
print(stack.pop())
else:
print("-1")
elif w[0]=='size':
print(len(stack))
elif w[0]=='empty':
if stack:
print("0")
else:
print("1")
elif w[0]=='top':
if stack:
print(stack[-1])
else:
print("-1")
주의할 점은, push 일때만 push 숫자 이렇게 받기 때문에 기본적으로 공백을 기준으로 받아야하고, 0인덱스에는 명령을, 1인덱스에는 숫자를 받아야한다.