주어진 리스트를 R 혹은 D가 저장되어있는 commands를 따라 뒤집거나 삭제를 하는 문제입니다.
요구사항은 어렵지 않으나, 리스트를 뒤집을 때 실제 리스트를 만들어 reverse로 뒤집으면 당연히 시간초과가 납니다.
또, 리스트를 주는 것이 아니라 String 형식으로 주기 때문에 문제를 풀기 위해 리스트로 변경해야 합니다.
이렇게 풀면 실제 리스트를 뒤집지 않고 요구사항을 만족시킬 수 있습니다.
주의사항
리스트를 그대로 반환하면 안됩니다. 출력 형식에 나와있는대로 리스트에 공백이 생기면 안되기 때문에 String 형식으로 출력을 해줬습니다.
import sys
import re
from collections import deque
def solution(commands, arr):
    # 현재 가리키고 있는 방향, 1 = front, -1 = rear
    pointer = 1
    for command in commands:
        # 현재 가리키고 있는 방향을 뒤집는다.
        if command == "R":
            pointer = -pointer
        # 현재 가리키고 있는 방향에서 pop
        else:
            if not arr:
                return False
            if pointer == 1:
                arr.popleft()
            else:
                arr.pop()
    return list(arr) if pointer == 1 else list(reversed(arr))
input = sys.stdin.readline
for _ in range(int(input())):
    commands = input().rstrip()
    int(input())
    arr = deque(map(str, re.findall("\d+", input().rstrip())))
    arr = solution(commands, arr)
    print("error") if arr == False else print("[" + ",".join(arr) + "]")