Hdu3328 (FLIP card)

Source: Internet
Author: User

Problem Description
Little Bobby Roberts (son of Big Bob, of Problem G) plays this solitaire memory game called Flipper. he starts with n cards, numbered 1 through n, and lays them out in a row with the cards in order left-to-right. (Card 1 is on the far left; card n is on the far right .) some cards are face up and some are face down. bobby then performs n-1 flips-either right flips or left flips. in a right flip he takes the pile to the far right and flips it over onto the card to its immediate left. for example, if the rightmost pile has cards A, B, C (from top to bottom) and card D is to the immediate left, then flipping the pile over onto card D wowould result in a pile of 4 cards: C, B, A, D (from top to bottom ). A left flip is analogous.

The very last flip completed MED will result in one pile of cards-some face up, some face down. for example, suppose Bobby deals out 5 cards (numbered 1 through 5) with cards 1 through 3 initially face up and cards 4 and 5 initially face down. if Bobby performs 2 right flips, then 2 left flips, the pile will be (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.

Now Bobby is very sharp and you can ask him what card is in any position and he can tell you !!! You will write a program that matches Bobby's amazing feat.


Input
Each test case will consist of 4 lines. the first line will be a positive integer n (2 ≤ n ≤100) which is the number of cards laid out. the second line will be a string of n characters. A character U indicates the corresponding card is dealt face up and a character D indicates the card is face down. the third line is a string of n-1 characters indicating the order of the flips Bobby performs. each character is either R, indicating a right flip, or L, indicating a left flip. the fourth line is of the form m q1 q2... qm, where m is a positive integer and 1 ≤ qi ≤ n. each qi is a query on a position of a card in the pile (1 being the top card, n being the bottom card ). A line containing 0 indicates end of input.


Output
Each test case shocould generate m + 1 lines of output. The first line is of the form

Pile twhere t is the number of the test case (starting at 1). Each of the next m lines shoshould be of the form

Card qi is a face up k. or

Card qi is a face down k. accordingly, for I = 1,..., m, where k is the number of the card.
For instance, in the above example with 5 cards, if qi = 3, then the answer wocould be

Card 3 is a face up 4.

Sample Input
5
UUUDD
RRLL
5 1 2 3 4 5
10
UUDDUUDDUU
LLLRRRLRL
4 3 7 6 1
0

Sample 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.
In fact, this question is not difficult, but it is difficult to understand the meaning of the question. First, give the initial status of each card. U is facing up, D is facing down, and then follow several operations, L indicates turning from the left, R indicates turning from the right, for example, the first example. The first two R indicates turning 5 to the right, stacked to 4, then the two sheets are stacked and flipped together, stacked to the remaining two L on 3, representing 1 on the far left, stacked to 2, and then flipped together, stacked on stacks 3, 4, and 5, then they become a bunch of last n queries, ask the status of the cards in the stack, for example, Card 1 is a face down 2. that is to say, the first card of the heap is facing down, and the number is 2.

# Include <stdio. h> struct card {int data; char dir; // face up or down}; struct card stack [105] [105]; int had, end, top [105]; void Turn (char turn [], int n) {int I = 0, tp; while (I <n) // each step {if (turn [I] = 'R') // flip a heap from right to left {tp = top [end]; // Number of while (tp> 0) in the stack to be flipped) // put all from top to bottom in the next stack in order {if (stack [end] [tp]. dir = 'U') stack [end] [tp]. dir = 'D'; else stack [end] [tp]. dir = 'U'; stack [end-1] [++ top [end-1] = stack [end] [tp]; tp --;} top [end] = 0; end --; // end is a stack pointing to the rightmost} else {tp = top [had]; while (tp> 0) {if (stack [had] [tp]. dir = 'U') stack [had] [tp]. dir = 'D'; else stack [had] [tp]. dir = 'U'; stack [had + 1] [++ top [had + 1] = stack [had] [tp]; tp --;} top [had] = 0; had ++; // had is a stack pointing to the leftmost} I ++; // execute next} int main () {int n, i, m, a [105], t = 1, k; char turn [105]; while (scanf ("% d", & n)> 0 & n) {getchar (); for (I = 1; I <= n; I ++) // first, each stack has only one card {scanf ("% c ", & stack [I] [1]. dir); stack [I] [1]. data = I; top [I] = 1;} getchar (); scanf ("% s", turn); // enter the step scanf ("% d ", & m); for (I = 1; I <= m; I ++) // enter the scanf ("% d ", & a [I]); had = 1; end = n; // Turn (turn, n-1) at the initial position ); // execute printf ("Pile % d \ n", t ++); for (I = 1; I <= m; I ++) {k = top [end]-a [I] + 1; if (stack [end] [k]. dir = 'U') printf ("Card % d is a face up % d. \ n ", a [I], stack [end] [k]. data); else printf ("Card % d is a face down % d. \ n ", a [I], stack [end] [k]. data) ;}}# include <stdio. h> struct card {int data; char dir; // face up or down}; struct card stack [105] [105]; int had, end, top [105]; void Turn (char turn [], int n) {int I = 0, tp; while (I <n) // each step {if (turn [I] = 'R') // flip a heap from right to left {tp = top [end]; // Number of while (tp> 0) in the stack to be flipped) // put all from top to bottom in the next stack in order {if (stack [end] [tp]. dir = 'U') stack [end] [tp]. dir = 'D'; else stack [end] [tp]. dir = 'U'; stack [end-1] [++ top [end-1] = stack [end] [tp]; tp --;} top [end] = 0; end --; // end is a stack pointing to the rightmost} else {tp = top [had]; while (tp> 0) {if (stack [had] [tp]. dir = 'U') stack [had] [tp]. dir = 'D'; else stack [had] [tp]. dir = 'U'; stack [had + 1] [++ top [had + 1] = stack [had] [tp]; tp --;} top [had] = 0; had ++; // had is a stack pointing to the leftmost} I ++; // execute next} int main () {int n, i, m, a [105], t = 1, k; char turn [105]; while (scanf ("% d", & n)> 0 & n) {getchar (); for (I = 1; I <= n; I ++) // first, each stack has only one card {scanf ("% c ", & stack [I] [1]. dir); stack [I] [1]. data = I; top [I] = 1;} getchar (); scanf ("% s", turn); // enter the step scanf ("% d ", & m); for (I = 1; I <= m; I ++) // enter the scanf ("% d ", & a [I]); had = 1; end = n; // Turn (turn, n-1) at the initial position ); // execute printf ("Pile % d \ n", t ++); for (I = 1; I <= m; I ++) {k = top [end]-a [I] + 1; if (stack [end] [k]. dir = 'U') printf ("Card % d is a face up % d. \ n ", a [I], stack [end] [k]. data); else printf ("Card % d is a face down % d. \ n ", a [I], stack [end] [k]. data );}}}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.