hdu 3328 Flipper(栈模拟)

来源:互联网 发布:小猪cms是干什么的 编辑:程序博客网 时间:2024/05/18 03:12

Flipper

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 630    Accepted Submission(s): 384


Problem Description
Little Bobby Roberts (son of Big Bob, of Problem G) plays this solitaire memory game called Flipper. He starts withn 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; cardn is on the far right.) Some cards are face up and some are face down. Bobby then performsn - 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 would result in a pile of 4 cards: C, B, A, D (from top to bottom). A left flip is analogous.

The very last flip performed 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 integern (2 ≤ n ≤ 100) which is the number of cards laid out. The second line will be a string ofn 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 ofn - 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 formm q1 q2 . . . qm, where m is a positive integer and 1 ≤qin. 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 should generate m + 1 lines of output. The first line is of the form
Pile t
where t is the number of the test case (starting at 1). Each of the nextm lines should 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 would be
Card 3 is a face up 4.
 

Sample Input
5UUUDDRRLL5 1 2 3 4 510UUDDUUDDUULLLRRRLRL4 3 7 6 10
 

Sample Output
Pile 1Card 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 2Card 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.
 

Source
East Central North America 2009
题目分析:读懂题栈模拟即可,很容易,记录好状态
 
#include <iostream>#include <algorithm>#include <cstring>#include <cstdio>#include <stack>#define MAX 107using namespace std;int n,m;char card[MAX];char op[MAX];int l,r;int state[MAX];stack<int> s[MAX];int ans[MAX];int main ( ){    int c = 1;    while ( ~scanf ( "%d" , &n ) , n )     {        scanf ( "%s" , card+1 );        scanf ( "%s" , op+1 );        l = 1 , r = n;        for ( int i = 1 ; i <= n ; i++ )            state[i] = card[i]=='U'?1:0;        for ( int i = 1 ; i <= n ; i++ )            s[i].push ( i );        for ( int i = 1 ; i < n ; i++ )        {            if ( op[i] == 'R' )            {                while ( !s[r].empty() )                {                    s[r-1].push ( s[r].top() );                    state[s[r].top()] ^= 1;                    s[r].pop();                }                r = r-1;            }            else            {                while ( !s[l].empty() )                {                    s[l+1].push ( s[l].top() );                    state[s[l].top()] ^= 1;                    s[l].pop();                }                l = l+1;            }        }        int cnt = 1;        while ( !s[l].empty() )        {            ans[cnt++] = s[l].top();            s[l].pop();        }        int u;        scanf ( "%d" , &m );        printf ( "Pile %d\n" , c++ );        while ( m-- )        {            scanf ( "%d" , &u );            printf ( "Card %d is a face %s %d.\n" ,  u , state[ans[u]]?"up":"down" , ans[u] );        }    }}


0 0
原创粉丝点击