Flipper

Time limit1sMemory limit128 MB

Summary
Simulate two-ended pile flips on a row of cards, then report the card number and orientation at queried positions.
Level

Medium5 of 10

Topics
Simulation, Implementation, Array
Solved
No attempts yet

Problem

Flipper is a solitaire memory game. You start with nn cards, numbered 1 through nn, laid out in a row in order from left to right (card 1 at the far left, card nn at the far right). Some cards are face up and some are face down.

You then perform n−1n - 1 flips, each either a right flip or a left flip. In a right flip you take the pile at the far right and flip it over onto the pile immediately to its left. Flipping a pile over reverses the order of its cards and turns every card over (a face-up card becomes face down, and vice versa), and the flipped cards land on top of the pile they are dropped onto. For example, if the rightmost pile is A, B, C (from top to bottom) and card D is immediately to its left, then flipping that pile onto D produces a single pile of four cards C, B, A, D (from top to bottom). A left flip is analogous: you take the pile at the far left and flip it over onto the pile immediately to its right.

After the last flip there is a single pile, some cards face up and some face down. For example, deal out 5 cards (numbered 1 through 5) with cards 1, 2, 3 initially face up and cards 4, 5 initially face down. Performing 2 right flips and then 2 left flips leaves the pile, from top to bottom: a face-down 2, a face-up 1, a face-up 4, a face-down 5, and a face-up 3.

Write a program that, after the flips, can report which card lies at any queried position.

Input

Each test case consists of four lines. The first line contains a positive integer nn (2≤n≤1002 \le n \le 100), the number of cards laid out. The second line is a string of nn characters: U means the card in that position is dealt face up and D means face down. The third line is a string of n−1n - 1 characters giving the order of the flips: R for a right flip and L for a left flip. The fourth line has the form m q1 q2 ... qm, where mm is a positive integer and each qiq_i (1≤qi≤n1 \le q_i \le n) queries a position in the final pile (position 1 is the top card, position nn is the bottom card).

A line containing a single 0 indicates the end of the input.

Output

Each test case produces m+1m + 1 lines of output. The first line has the form

Pile t

where tt is the test case number (starting at 1). Each of the next mm lines, for i=1,…,mi = 1, \ldots, m, has the form

Card qi is a face up k.

or

Card qi is a face down k.

according to whether the card at position qiq_i ends face up or face down, where kk is that card's number. For example, in the 5-card case above, if qi=3q_i = 3 the answer is Card 3 is a face up 4.

Examples2

  1. Example 1

    Input
    5
    UUUDD
    RRLL
    5 1 2 3 4 5
    10
    UUDDUUDDUU
    LLLRRRLRL
    4 3 7 6 1
    0
    
    Expected output
    Pile 1
    Card 1 is a face down 2.
    Card 2 is a face up 1.
    Card 3 is a face up 4.
    Card 4 is a face down 5.
    Card 5 is a face up 3.
    Pile 2
    Card 3 is a face down 1.
    Card 7 is a face down 9.
    Card 6 is a face up 7.
    Card 1 is a face down 5.
    
  2. Example 2

    Input
    2
    UD
    R
    2 1 2
    0
    
    Expected output
    Pile 1
    Card 1 is a face up 2.
    Card 2 is a face up 1.