AC

Interview

Time limit1sMemory limit256 MB

Summary
Apply a string of reverse (R) and discard-first (D) operations to an integer array, printing the result or error if D hits an empty array.
Level

Medium4 of 10

Topics
Implementation, Array, Two pointers
Solved
No attempts yet

Problem

Sunyeong built a small language called AC for manipulating integer arrays. The language has exactly two functions, R and D.

  • R (reverse): reverses the order of the numbers in the array.
  • D (discard): removes the first number of the array. Using D on an empty array raises an error.

Functions can be chained and applied together. For example, RDD reverses the array once and then discards the first two numbers.

Given the initial array and a string of functions to run, write a program that outputs the final array after all functions have been applied.

Input

The first line contains the number of test cases TT. (T≤100T \le 100)

Each test case consists of three lines:

  • The first line contains the function string pp, made up only of the characters R and D, with 1≤∣p∣≤1000001 \le |p| \le 100000.
  • The second line contains nn, the number of elements in the array. (0≤n≤1000000 \le n \le 100000)
  • The third line contains the array elements in the form [x1,x2,...,xn]. (1≤xi≤1001 \le x_i \le 100) An empty array is given as [].

Over all test cases, the sum of the lengths of pp and the sum of nn each do not exceed 700,000.

Output

For each test case, print the resulting array after applying every function, in the form [x1,x2,...,xn] on a single line, with elements separated by commas and no spaces. If an error occurs because D is applied to an empty array at any point, print error instead.

Examples2

  1. Example 1

    Input
    4
    RDD
    4
    [1,2,3,4]
    DD
    1
    [42]
    RRD
    6
    [1,1,2,3,5,8]
    D
    0
    []
    
    Expected output
    [2,1]
    error
    [1,2,3,5,8]
    error
    
  2. Example 2

    Input
    1
    R
    3
    [1,2,3]
    
    Expected output
    [3,2,1]