Flipper(栈)

Flipper

Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 12 Accepted Submission(s) : 8
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 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. 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.

Source
East Central North America 2009


题意:

模拟题,主要是题意难懂,这里只说一下题目中说的5张卡片的结果是怎么来的,首先在桌面上面从左到右把5张卡片放好,有的是面朝上up,有的是面朝下down,这里1,2 ,3都是up,4 ,5是down。‘R’操作就是把最右侧down的卡片5翻转成up放在4上面,下一次的'R'是把卡片4 和 5 一起翻转放在3上面;'L'就是把up的1翻转成down放到2上面,下一次的'L'还是把1 和2 一起翻转,这样5张卡片进行4次操作就放在了一堆上面,这样从上到下就是题目中输出的顺序和样子了。

题解:将n张牌自身当做一栈,所以每次变换若为“L”变换则统计“L”变换的个数

           每次变换后添加至后面的一栈中,由于栈的特殊性(后进先出),相当于调换了次序,“R”变换类似,

           每次变换后添加至前一栈,由于题目的变换次数为n-1次,则最后得到的栈即为所求。。。


          即为最终变换结果

#include<cstdio>
#include<stack>
#include<cstring>
using namespace std;
const int M=105;
stack<int>s[M];
int main()
{
    int n,m,i,j,k,num=1,a[M],ok[M];
    char str[M];
    while(scanf("%d",&n),n)
    {
        scanf("%s",str);
        for(i=0;i<n;i++)
        {
            if(str[i]=='U')
             ok[i]=1;
            else
             ok[i]=0;
        }
        int l=0,r=n-1;
        for(i=0;i<n;i++)
         s[i].push(i);
        scanf("%s",str);
        for(i=0;i<n-1;i++)
        {
            if(str[i]=='L')
            {
                l++;
                for(j=0;j<l;j++)
                 ok[j]=1-ok[j];
                while(!s[l-1].empty())
                {
                    s[l].push(s[l-1].top());
                    s[l-1].pop();
                }
            }
            else
            {
                r--;
                for(j=n-1;j>r;j--)
                 ok[j]=1-ok[j];
                while(!s[r+1].empty())
                {
                    s[r].push(s[r+1].top());
                    s[r+1].pop();
                }
            }
        }
        for(i=0;i<n;i++)
        {
            a[i]=s[l].top();
            s[l].pop();
        }
        printf("Pile %d\n",num++);
        scanf("%d",&m);
        while(m--)
        {
            scanf("%d",&k);
            printf("Card %d is a face ",k--);
            printf(ok[a[k]]?"up ":"down ");
            printf("%d.\n",a[k]+1);
        }

    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值