Flipper

Flipper

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 65 Accepted Submission(s): 52
 
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 ≤ 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 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
 
Recommend
teddy
 
/*
题意:阅读题题目有点坑,实际上就是模拟了一个翻牌的过程,首先n张牌平铺在桌面上的,没有重叠,每张给出正反面的状态
    ,然后有两种操作:R表示把最右边的一摞牌整体反转,全部放在右边第2张的牌上,L操作就是和R操作类似的,就是相反。
    然后给出m次询问,问第i张牌的正反状态和翻转的次数
初步思路:模拟队列嘛,题意想出来了,就差不多了。操作最坏的情况是1e6,查询建一个映射就可以了

#错误:读错题了,不是反转次数,而是从上往下数是第q张牌,是几号牌,并且正反状态
    反转的地方写的不对。
    
#感悟:调了两个小时的错,终于没辜负我,1Y
*/
#include<bits/stdc++.h>
using namespace std;
int n,m;
int q;
int Sym[110];//存储的每张牌的正反状态
string Symbol[2]={"up","down"};
int l,r;//表示的是左右需要反转的指针;
int ca=1;
int card[110];//存储序列号码的顺序
int tmp_card[110];//用来转移牌的中间数组
string Frist_Symbol;//初始的所有牌的状态
string Operation;//需要进行的操作
void init(){
    for(int i=0;i<=n;i++){
        card[i]=i;
    }
    l=1;r=n;
}
void turn(char op,int &l,int &r){//反转函数
    if(op=='R'){//翻右边的
        // cout<<"翻右边"<<endl;
        //每张牌进行反转
        for(int i=r;i<=n;i++){
            Sym[card[i]]^=1;
        }
        //将这摞牌整过反转
        for(int i=r;i<=(r+n)/2;i++){                    
            //cout<<card[i]<<" "<<card[n-(i-r)]<<endl;
            int tmp=card[i];
            card[i]=card[n-(i-r)];              //#出错 #修复 #再次修复 应该是n-(i-r)
            card[n-(i-r)]=tmp;
            
        }
        // for(int i=1;i<=n;i++){
                // cout<<card[i]<<" ";
            // }
            // cout<<endl;
        //将这摞牌放到r-1张牌的上边
        /*
        #错误:这里不应该是放在r-1,而是放在左边第一堆的上面
        
        只有最后一次的时候才是将两堆放在一块
        */
        if(l+1!=r){//不是最后一次
            int tmp=card[r-1];
            for(int i=r-1;i<=n-1;i++){
                card[i]=card[i+1];
            }
            card[n]=tmp;
        }else{                                 //#出错
            //最后一次
            for(int i=1;i<=l;i++){
                tmp_card[i]=card[i];
            }//中间专业数组
            //cout<<"ok"<<endl;
            for(int i=r;i<=n;i++){             //#手残 i写成了r
                card[i-r+1]=card[i];
            }
            //cout<<"ok"<<endl;
            for(int i=(n-r+2);i<=n;i++){
                //cout<<i<<" "<<i-(n-r+2)+1<<endl;
                card[i]=tmp_card[i-(n-r+2)+1];
            }
        }
        // for(int i=1;i<=n;i++){
                // cout<<card[i]<<" ";
            // }
            // cout<<endl;
        r--;
    }else{//翻左边的
        //cout<<"翻左边"<<endl;
        //每张牌进行反转
        for(int i=1;i<=l;i++){
            Sym[card[i]]^=1;
        }
        //将这摞牌整过反转
        for(int i=1;i<=(1+l)/2;i++){
            int tmp=card[i];
            card[i]=card[l-i+1];
            card[l-i+1]=tmp;
        }
        //将这摞牌放到l+1张牌的上边
        /*
        #错误:这里不应该是放在l+1,而是放在右边第一堆的上面
        
        只有最后一次的时候才是将两堆放在一块,放在一起实际上就是没有变化
        */
        l++;
    }
}
int main(){
    // freopen("in.txt","r",stdin);
    while(scanf("%d",&n)!=EOF&&n){
        getchar();
        init();
        printf("Pile %d
",ca++);
        cin>>Frist_Symbol;
        for(int i=0;i<Frist_Symbol.size();i++){//将没张牌的状态装到数组中
            Sym[i+1]=Frist_Symbol[i]=='U'?0:1;
        }
        // for(int i=1;i<=n;i++){
                // cout<<Sym[i]<<" ";
            // }
            // cout<<endl;
        cin>>Operation;
        // cout<<Operation<<endl;
        for(int i=0;i<Operation.size();i++){
            turn(Operation[i],l,r);
            
            // for(int i=1;i<=n;i++){
                // cout<<Sym[i]<<" ";
            // }
            // cout<<endl;
            //cout<<l<<" "<<r<<endl;
            // for(int i=1;i<=n;i++){
                // cout<<card[i]<<" ";
            // }
            // cout<<endl;
        }
        scanf("%d",&m);
        while(m--){
            scanf("%d",&q);
            cout<<"Card "<<q<<" is a face "<<Symbol[Sym[card[q]]]<<" "<<card[q]<<".
";
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/wuwangchuxin0924/p/6366548.html