https://www.acmicpc.net/problem/10845
Code
from sys import stdin
n = int(input())
queue = []
for i in range(n):
command = stdin.readline().split()
if command[0] == 'push':
queue.append(int(command[1]))
elif command[0] == 'pop':
if queue: print(queue.pop(0))
else: print(-1)
elif command[0] == 'size':
print(len(queue))
elif command[0] == 'empty':
print(int(len(queue)==0))
elif command[0] == 'front':
if queue: print(queue[0])
else: print(-1)
elif command[0] == 'back':
if queue: print(queue[-1])
else: print(-1)
참고
대량의 데이터를 반복적으로 입력 받을 때, input()
보다 sys.stdin.readline()
이 더 빠르다.
sys.stdin.readline()
은 줄바꿈(\n)이 있는 통째로 받아오는 반면, input()
은 문자열 변환 줄바꿈(\n) 제거 등 처리로 더 느리다.