Hdu 1022 Train Problem I (模拟栈)

http://acm.hdu.edu.cn/showproblem.php?pid=1022

火车进出模拟。开始WA是因为依次循环结束后栈没有清空。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include <stack>
#include <algorithm>
using namespace std;
int main()
{
    int n,i,j;
    string str1,str2;
    stack <char> t;
    while(scanf("%d",&n)==1)
    {
        cin>>str1>>str2;
        int l=str1.length();
        int c[l*2],k=0;
        memset(c,0,sizeof(c));
        for(i=0,j=0; i<=l&&j<l;)
        {
            if(!t.empty()&&t.top()==str2[j])
            {
                t.pop();
                j++;
                c[k++]=0;
            }
            else
            {
                if(i>=l) break;
                t.push(str1[i]);
                i++;
                c[k++]=1;
            }
        }
       // cout<<i<<" "<<j<<"\n";
        if(j==l)
        {
            printf("Yes.\n");
            for(int i=0; i<k; i++)
            {
                if(c[i])printf("in\n");
                else printf("out\n");
            }
        }
        else
        {
            printf("No.\n");
        }
        printf("FINISH\n");
        while(!t.empty()) t.pop();
    }
    return 0;
}

 

posted @ 2016-03-14 22:33  cherry_yue  阅读(63)  评论(0)    收藏  举报