전.py

[python] 백준 10828 스택

jeonnew 2022. 1. 24. 15:42
728x90

https://www.acmicpc.net/problem/10828

 

10828번: 스택

첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 10,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 100,000보다 작거나 같다. 문제에 나와있지

www.acmicpc.net

import sys

n = int(input())
result = []
stack = []
for i in range(n):
    s = sys.stdin.readline().strip()
    if s[0] == "p":
        if s[:4] == "push":
            stack.append(s[5:])
        elif s[:3] == "pop":
            if len(stack) > 0:
                print(stack.pop())
            else:
                print(-1)
    elif s[0] == "s":
        print(len(stack))
    elif s[0] == "e":
        if len(stack) == 0:
            print(1)
        else:
            print(0)
    elif s[0] == "t":
        if len(stack) > 0:
            print(stack[-1])
        else:
            print(-1)

 

반응형