hdu 3328 Flipper详解(栈的应用)

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 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 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(这句话说有n-1个操作,所以可知最后只剩下一堆牌,牌数为n)。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 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 next m 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
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.
Sponsor

//解题思路:栈的应用
//灵感来源于https://blog.csdn.net/steamqueen/article/details/7730304

#include<stack>
#include<string.h>
#include<stdio.h>
#define M 105
using namespace std;
int v[M];
int main()
{
    stack<int> s[M];
    char state[M];
    int n,i,j;
    char operate[M];
    int t=1;
    int put[M];
    while(scanf("%d",&n)!=EOF)
    {
        if(n==0) break;
        //input
        scanf("%s",state);
        scanf("%s",operate);
        int m;
        scanf("%d",&m);

        //记录最开始的翻转情况
        for(i=0; i<n; ++i){
            if(state[i]=='U'){
                v[i] = 1;
            }
            else
                v[i] = 0;
        }
        for(i=0; i<n; ++i)  //把每张牌都看成一个栈,存放不同的牌(0~n-1)
            s[i].push(i);

        int left = 0,right = n-1;
        for(i=0; i<n; ++i){ //把要翻转的翻转过来,出栈后放到另一个栈顶
            if(operate[i]=='L'){
                left++;
                for(j=0; j<left; ++j){ //exchange
                    if(v[j]==1) v[j] = 0;
                    else v[j]=1;
                }
                while(!s[left-1].empty()){
                    s[left].push(s[left-1].top());
                    s[left-1].pop();
                }
            }
            else if(operate[i]=='R')
            {
                right--;
                for(j=right+1; j<n; ++j){ //exchange
                    if(v[j]==1) v[j]=0;
                    else v[j]=1;
                }
                while(!s[right+1].empty()){
                    s[right].push(s[right+1].top());
                    s[right+1].pop();
                }
            }
        }

        for(i=0; i<n; ++i){  //把栈内数据存到数组里,用
            put[i]=s[left].top();
            s[left].pop();
        }

        printf("Pile %d\n",t);
        t++;
        int k;
        while(m--)
        {
            scanf("%d",&k);  //取一个待查找的层数
            /*题中的描述Each qi is a query on a position of a card in the pile (1 being the top card, n being the bottom card).*/
            printf("Card %d is a face ",k);   k--;
            if(v[put[k]]==1)
                printf("up ");
            else
                printf("down ");

            printf("%d.\n",put[k]+1); //注意加一因为(牌上有数字)先前为了方便用的是0~n-1的牌,题中是1~n
            
        }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

葛济维的博客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值