77 lines
2.2 KiB
Python
77 lines
2.2 KiB
Python
# 에디터
|
|
|
|
import sys
|
|
from collections import deque
|
|
|
|
input = sys.stdin.readline
|
|
|
|
def l(left_q, right_q):
|
|
if len(left_q) >= 1:
|
|
right_q.appendleft(left_q.pop())
|
|
return
|
|
|
|
def d(left_q, right_q):
|
|
if len(right_q) >= 1:
|
|
left_q.append(right_q.popleft())
|
|
return
|
|
|
|
def b(left_q):
|
|
if len(left_q) >= 1:
|
|
left_q.pop()
|
|
return
|
|
|
|
def p(left_q, w):
|
|
left_q.append(w)
|
|
return
|
|
|
|
COMMANDS = {
|
|
"L": l,
|
|
"D": d,
|
|
"B": b,
|
|
"P": p,
|
|
}
|
|
|
|
def excute(left_q, right_q, *c):
|
|
command = COMMANDS.get(c[0], None)
|
|
if c[0] == "P":
|
|
command(left_q, c[1])
|
|
elif c[0] == "L" or c[0] == "D":
|
|
command(left_q, right_q)
|
|
elif c[0] == "B":
|
|
command(left_q)
|
|
else:
|
|
return
|
|
return
|
|
|
|
def solution():
|
|
s = input().rstrip()
|
|
m = int(input().rstrip())
|
|
|
|
left_q = deque(list(s))
|
|
right_q = deque([])
|
|
|
|
for _ in range(m):
|
|
excute(left_q, right_q, *(input().rstrip().split()))
|
|
|
|
result = list(left_q) + list(right_q)
|
|
print("".join(result))
|
|
|
|
return
|
|
|
|
|
|
solution()
|
|
|
|
"""
|
|
걸린 시간: 30분
|
|
|
|
시간 복잡도: deque는 pop이나 append가 어디든 O(1)이므로 명령어 수행은 항상 O(1)이고, m번 명령을 반복하기 때문에 O(m)이다.
|
|
그 후 마지막에 두 deque를 합쳐서 출력하기 때문에 n개의 단어에서 m만큼 글자가 추가 되었다면 O(n+m)이다.
|
|
따라서 전체 시간복잡도는 O(n+m)이다.
|
|
|
|
해설: 커서를 기준으로 양쪽에 deque를 배치한 후 l, d로 커서를 움직이면 양쪽 deque에서 요소들을 옮기고, b, p로 요소를 지우거나 추가하면
|
|
커서 기준 왼쪽으로 동작한다고 했으므로 왼쪽 deque로 요소를 조정한다. 마지막에 두 deque를 list로 만들어서 합치고 출력하면 끝
|
|
|
|
확장성을 위해 dispatch 전략을 사용했지만 l,d,b,p의 인자가 달라서 excute 함수에서 또 분기를 했다. -> l,d,b,p의 인자를 같게 하면 됨
|
|
자료구조가 deque에서 다른 것으로 바뀐다면 다 수정해야 하기 때문에 결합도가 너무 높다.
|
|
-> 추상화 계층 하나를 추가해서 Editor라는 클래스를 만들고 거기다가 기능들 쓴 다음에 l,d,b,p가 그걸 받아오면 나중에 Editor 클래스만 바꾸면 됨
|
|
""" |