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을 하는 위치를 바꿔주고, 최종 결과를 출력을 할때에도, 구분을 해줬다.
'알고리즘 > 백준' 카테고리의 다른 글
[BOJ/백준] 11780 플로이드 2 (0) | 2021.09.02 |
---|---|
[BOJ/백준] 7511 소셜 네트워킹 어플리케이션 (0) | 2021.09.02 |
[BOJ/백준] 4095 최대 정사각형 (0) | 2021.09.02 |
[BOJ/백준] 3673 나눌 수 있는 부분 수열 (0) | 2021.09.02 |
[BOJ/백준] 2023 신기한 소수 (0) | 2021.09.02 |