import sys
from collections import deque
def input():
return sys.stdin.readline().rstrip()
def pop(queue,flag):
if flag:
return queue.pop()
else:
return queue.popleft()
T = int(input())
for _ in range(T):
commands = list(input())
N = int(input())
if N:
arr = deque(input().replace('[','').replace(']','').split(','))
else:
input()
arr = deque()
# 0 정방향 1 역방향
flow = 0
flag = True
for command in commands:
if command == 'R':
flow = (flow+1)%2
else:
if arr:
pop(arr,flow)
else:
flag = False
break
if flag:
if flow:
arr.reverse()
print(f'[{",".join(arr)}]')
else:
print('error')

 

이 문제는 deque를 통해, 역방향인지 정방향인지에 따라 나눠서 결정을 지으면 되는 문제였다.

 

정방향인지 역방향인지에 따라 pop을 하는 위치를 바꿔주고, 최종 결과를 출력을 할때에도, 구분을 해줬다.

+ Recent posts