[알고리즘/백준] 1406: 에디터(python)

유현민·2022년 4월 4일
0

알고리즘

목록 보기
92/253

처음에는 커서의 위치를 숫자로 주면서 계속 변화시켰다. 그랬더니 시간 초과가 나버린... 고민고민하다가 스택 두개로 구현하면 될 것 같아서 해보았다. 스택을 두 개 준비하고 그 사이에 커서가 있다고 생각한다. 왼쪽으로 커서를 옮기면 왼쪽 스택에서 pop을 하고 오른쪽 스택에 append를 해준다... 이런 식으로 하면 풀린다!! 마지막에 합치는 건 오른쪽 스택을 뒤집어 줘야 한다.

from sys import stdin


stack1 = list(map(str, stdin.readline().strip()))
stack2 = []

for _ in range(int(stdin.readline().strip())):
    a = list(stdin.readline().strip().split())
    if a[0] == 'L':
        if stack1:
            stack2.append(stack1.pop())

    elif a[0] == 'D':
        if stack2:
            stack1.append(stack2.pop())

    elif a[0] == 'B':
        if stack1:
            stack1.pop()

    else:
        stack1.append(a[1])
stack1.extend(reversed(stack2))
print(''.join(stack1))
profile
smilegate megaport infra

0개의 댓글