10866번: 덱
첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 10,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 100,000보다 작거나 같다. 문제에 나와있지
www.acmicpc.net
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
import sys
from collections import deque
n = int(input())
dq = deque([])
for i in range(n):
command = list(map(str,sys.stdin.readline().split()))
if command[0] == "push_front":
dq.appendleft(command[1])
elif command[0] == "push_back":
dq.append(command[1])
elif command[0] == "pop_front":
if not dq:
print(-1)
else:
print(dq.popleft())
elif command[0] == "pop_back":
if not dq:
print(-1)
else:
print(dq.pop())
elif command[0] == "size":
print(len(dq))
elif command[0] == "empty":
if not dq:
print(1)
else:
print(0)
elif command[0] == "front":
if not dq:
print(-1)
else:
print(dq[0])
elif command[0] == "back":
if not dq:
print(-1)
else:
print(dq[-1])
|
cs |
'백준' 카테고리의 다른 글
백준 10828번: 스택 (0) | 2021.09.30 |
---|---|
백준 10845번: 큐 (0) | 2021.09.30 |
백준 1920번: 수 찾기 (0) | 2021.09.29 |
백준 10989번: 수 정렬하기 3 (0) | 2021.09.29 |
백준 2231번: 분해합 (0) | 2021.09.29 |