코딩테스트

[백준] 10828 | 스택 [파이썬/python]

사용할수없는닉네임이왜이렇게많지ㅠㅠ 2025. 3. 31. 16:32
728x90

문제

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

스택을 구현하는 문제이다.

 

접근

if문으로 입력되는 명령을 구분해 처리하도록 구현했다.

 

코드

import sys
input = sys.stdin.readline

s = []

for _ in range(int(input())):
    tmp = input().split()
    if tmp[0] == 'push': s.append(tmp[1])
    elif tmp[0] == 'pop': print(s.pop() if s else -1)
    elif tmp[0] == 'size': print(len(s))
    elif tmp[0] == 'empty': print(0 if s else 1)
    elif tmp[0] == 'top': print(s[-1] if s else -1)
728x90