Flipper
Time limit1sMemory limit128 MB
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 cards, numbered 1 through , laid out in a row in order from left to right (card 1 at the far left, card at the far right). Some cards are face up and some are face down.
You then perform 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 (), the number of cards laid out. The second line is a string of characters: U means the card in that position is dealt face up and D means face down. The third line is a string of 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 is a positive integer and each () queries a position in the final pile (position 1 is the top card, position is the bottom card).
A line containing a single 0 indicates the end of the input.
Output
Each test case produces lines of output. The first line has the form
Pile t
where is the test case number (starting at 1). Each of the next lines, for , 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 ends face up or face down, where is that card's number. For example, in the 5-card case above, if the answer is Card 3 is a face up 4.