-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1406.py
More file actions
22 lines (21 loc) · 828 Bytes
/
1406.py
File metadata and controls
22 lines (21 loc) · 828 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import sys
words = list(sys.stdin.readline().rstrip())
sub = list()
n = int(sys.stdin.readline())
for i in range(n):
order = sys.stdin.readline().rstrip().split()
if order[0] == "L" and words:
sub.append(words.pop())
elif order[0] == "D" and sub:
words.append(sub.pop())
elif order[0] == "B" and words:
words.pop()
elif order[0] == "P":
words.append(order[1])
# while sub:
# words.append(sub.pop())
# print("".join(words))
print("".join(words + sub[::-1])) # 리스트를 역순으로 출력함으로써 stack에서 pop 하듯이 동작
# 시간 초과로 인해 문제 풀이에 어려움
# 반복문 내에서 input() 대신 sys.sdin.readLine() 사용
# list의 특정 인덱스 값을 제거 하는 것은 O(n)시간이 걸림 <-> pop() 은 O(1) // pop(index)는 O(n)