문제

입력 및 출력

실행 코드
from collections import deque
import sys
input = sys.stdin.readline
q = deque()
n = int(input())
for i in range(n):
check = input().split()
if check[0] == 'push':
q.append(check[1])
elif check[0] == 'pop':
if len(q) == 0:
print(-1)
else:
print(q.popleft())
elif check[0] == 'size':
print(len(q))
elif check[0] == 'empty':
if len(q) == 0:
print(1)
else:
print(0)
elif check[0] == 'front':
if len(q) == 0:
print(-1)
else:
print(q[0])
elif check[0] == 'back':
if len(q) == 0:
print(-1)
else:
print(q[-1])
풀이
- for문을 통해 입력된 문자에 해당하는 명령을 실행한다.
- 큐를 구현하려면
deque
라이브러리를 사용해야한다.
input
대신 sys.stdin.readlin
을 작성해야 시간초과가 안난다.
[문제 링크]
https://www.acmicpc.net/problem/18258