TIL/알고리즘

[알고리즘] Python | 백준 | 1406번: 에디터

yeoney 2023. 2. 21. 01:29
반응형
import sys

stack_1 = list(sys.stdin.readline().rstrip())
stack_2 = []
N = len(stack_1)
M = int(input())

for _ in range(M):
    command = sys.stdin.readline().split()
    if command[0]=="L" and stack_1:
        stack_2.append(stack_1.pop())
    elif command[0] == "D" and stack_2:
        stack_1.append(stack_2.pop())
    elif command[0]=="B" and stack_1:
        stack_1.pop()
    elif command[0] == "P":
        stack_1.append(command[1])

print("".join(stack_1 + list(reversed(stack_2))))
반응형