HDU 1022 Train Problem I

栈的基本操作……

#include <cstdio>
#include <cstring>
#include <stack>
using namespace std;
char in[100],out[100];
int flag[200];
int main(){
    int n,top,i,j,k,bo;
    while(scanf("%d",&n)!=EOF){
        stack s;
        scanf("%s%s",&in,&out);
        top=i=j=k=0; bo=1;
        while(j<n){
            if(in[i]==out[j]){
                flag[k++]=0; flag[k++]=1;
                i++; j++;
            }    
            else if((!s.empty())&&s.top()==out[j]){
                flag[k++]=1;
                s.pop(); j++;
            }  
            else if(i<n)s.push(in[i++]),flag[k++]=0;   
            else{bo=0;break;}
        }    
        if(bo==0)printf("No.\n");
        else{
            printf("Yes.\n");
            for(int p=0;p<k;p++){
                if(flag[p]==0)printf("in\n");
                else printf("out\n");
            }    
        }    
        printf("FINISH\n");
    }    
    return 0;
}    
posted @ 2014-04-16 15:31  forever97  阅读(129)  评论(0编辑  收藏  举报