import sys
data = list(input())
index = len(data)
for _ in range(int(input())):
command = input().split()
if command[0] == "P":
data.insert(index, command[1])
index += 1
elif command[0] == "L":
if index == 0:
continue
else:
index -= 1
elif command[0] == "B":
if index == 0:
continue
del data[index-1]
index -= 1
elif command[0] == "D":
if index >= len(data):
continue
index += 1
print("".join(data))
결과는 당연히 시간초과를 받았다.
import sys
input = sys.stdin.readline
stack1 = list(input().strip())
stack2 = []
for _ in range(int(input())):
command = input().split()
if command[0] == "P":
stack1.append(command[1])
elif command[0] == "L" and len(stack1) != 0:
stack2.append(stack1.pop())
elif command[0] == "D" and len(stack2) != 0:
stack1.append(stack2.pop())
elif command[0] == "B" and len(stack1) != 0:
stack1.pop()
print("".join(stack1 + list(reversed(stack2))))