알고리즘

[Algorithms] 백준 1406번: 에디터

gomduribo 2023. 8. 13. 13:16

백준 1406번은 간단한 에디터를 구현하는 문제였다.

처음에는 배열과 스택 하나만으로 해결하려고 했는데, 잘 안되어서 구글링해서 해결했다. 두개의 스택을 이용해서 커서와 같이 구현을 해놓은 코드를 이용했다.

 

import sys

stack_l = list(input())
stack_r = []
n = int(input())

for i in range(n):
    command = sys.stdin.readline().split()

    if command[0] == "L" and stack_l:
        stack_r.append(stack_l.pop())
    elif command[0] == "D" and stack_r:
        stack_l.append(stack_r.pop())
    elif command[0] == "B" and stack_l:
        stack_l.pop()
    elif command[0] == "P":
        stack_l.append(command[1])

print("".join(stack_l + list(reversed(stack_r))))