hdu Flipper (用栈模拟)

#include <stdio.h>
#include <string.h>
#include <stack>

using namespace std;
int T;
stack <int> s[105];

int main(){
    int flag[105];//0 - up,1 - down
    int n,i,j;
    int pl,pr,p;
    int ans[105];
    int outnum,out[105];
    char ud[105];
    char command[105];

    T = 1;
    while(1){
        memset(ud,'\0',sizeof(ud));
        memset(command,'\0',sizeof(command));
        memset(ans,0,sizeof(ans));
        memset(out,0,sizeof(out));
        memset(flag,0,sizeof(flag));

        for(i = 0;i < 105;i++){
            while(!s[i].empty()){
                s[i].pop();
            }
        }
        scanf("%d",&n);
        if(n == 0){
            break;
        }
        scanf("%s",ud);
        scanf("%s",command);
        scanf("%d",&outnum);
        for(i = 0;i < outnum;i++){
            scanf("%d",&out[i]);
        }
        pl = 1;
        pr = n;

        for(i = 0;i < n;i++){
            if(ud[i] == 'U'){
                flag[i+1] = 0;
            }
            else{
                flag[i+1] = 1;
            }
        }
        for(i = 1;i <= n;i++){
            s[i].push(i);
        }
        for(i = 0;i < n - 1;i++){
            if(command[i] == 'R'){
                while(!s[pr].empty()){
                    int tem = s[pr].top();
                    s[pr-1].push(tem);
                    flag[tem] = !flag[tem];
                    s[pr].pop();
                }
                pr--;
            }
            else{
                while(!s[pl].empty()){
                    int tem = s[pl].top();
                    s[pl + 1].push(tem);
                    flag[tem] = !flag[tem];
                    s[pl].pop();
                }
                pl++;
            }
        }
        p = pl;
        j = 1;
        while(!s[p].empty()){
            ans[j] = s[p].top();
            s[p].pop();
            j++;
        }
        printf("Pile %d\n",T);
        T++;
        for(i = 0; i < outnum;i++){
            int tem = out[i];
            if (flag[ans[tem]] == 0){
                printf("Card %d is a face up %d.\n",tem,ans[tem]);
            }
            else{
                printf("Card %d is a face down %d.\n",tem,ans[tem]);
            }
        }
    }

    return 0;
}

你可能感兴趣的:(hdu Flipper (用栈模拟))