[ds] 백준 #1406 에디터

문제

https://www.acmicpc.net/problem/1406

풀이

# 에디터
import sys
from collections import deque
input = sys.stdin.readline

left = deque(input())
right = deque()
m = int(input())
left.pop()
for _ in range(m):
    op = list(map(str, input().split()))
    if op[0] == 'P':
        left.append(op[1])
    elif op[0] == 'L':
        if left:
            right.appendleft(left.pop())
    elif op[0] == 'D':
        if right:
            left.append(right.popleft())
    else:
        if left:
            left.pop()
    print(left, right)
print(''.join(left)+''.join(right))

접근

커서를 직접 움직이는게 아니라 두개의 deque를 사용해서 커서를 기준으로 문자를 왼쪽 deque와 오른쪽 deque로 이동시킨다.

좋은 웹페이지 즐겨찾기