백준 알고리즘 기초 1/2, 200 - 자료구조1 스택

HyeonKi Jo·2022년 10월 19일
0
post-thumbnail

백준 알고리즘 기초 1/2. 200 - 자료구조1 (스택)

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

코드

# input을 sys.stdin.readline 함수로 써야 백준 시간제한에 걸리지 않습니다.
# 다만, 마지막에 '\n'까지 들어가서 문자열을 입력할 때는 input().strip() 이렇게 써야한다고 합니다.

import sys
input=sys.stdin.readline

arr = []
orders = {
    'push' : (lambda x: arr.append(x)),
    'pop' : (lambda x: print(arr.pop() if arr else -1)),
    'size' : (lambda x: print(len(arr))),
    'empty' : (lambda x: print(0 if arr else 1)),
    'top' : (lambda x: print(arr[-1] if arr else -1))
}

N = int(input())

for i in range(N):
    texts = input().strip().split(' ')
    texts.append("")
    answer = orders[texts[0]](texts[1])
profile
Talking Potato

0개의 댓글