[못 푼 문제] 백준 1406(스택/큐)

장준서·2022년 4월 1일
0

알고리즘 문제

목록 보기
18/29

평범한 문제라서 insert와 del을 사용해서 리스트를 수정할 수 있지만 0.3초라는 제한된 시간 속에서 너무 많은 시간( O(N) )을 잡아먹게 된다. 그럼으로 커서의 왼쪽 리스트와 커서의 오른쪽 리스트를 만들어 시간이 상대적으로 적은 append와 pop을 이용하여 문제를 해결한다. ( O(1) )

import sys

left = list(input())
n = int(input())
right = []
orders = []
for _ in range(n):
    orders.append(list(sys.stdin.readline().split()))

for order in orders:
    if order[0] == 'P':
        left.append(order[1])
    elif order[0] == 'L':
        if left:
            right.append(left.pop())
    elif order[0] == 'D':
        if right:
            left.append(right.pop())
    else:
        if left:
            left.pop()

right.reverse()
print(''.join(left) + ''.join(right))
profile
let's get ready to rumble

0개의 댓글