HDU 1022 stack 的应用

#include <iostream>
#include<stack>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<string>
using namespace std;
int N;
string come;
string leave;
int order[50];
int main()
{
    setbuf(stdout,NULL);
    stack<char>s;
    while(cin>>N)
    {
        cin>>come;
        cin>>leave;
        while(!s.empty())
        {
            s.pop();
        }
        int i=1,j=1,k=0;
        bool flag=true;
        s.push('A');//设置标志位,防止栈位空内存出错
        s.push(come[0]);
        order[0]=1;
        while(!(s.size()==1 && j==N) )
        {
            while(s.top()!=leave[k]&&j!=N)
            {
                s.push(come[j++]);
                order[i++]=1;
            }
            if(j==N && s.top()!=leave[k])
            {
                flag=false;
                break;
            }
            s.pop();
            k++;
            order[i++]=-1;
        }
        if(s.size()!=1|| flag==false)
        {
            printf("No.\n");
        }
        else
        {
            printf("Yes.\n");
            for(j=0;j<i;j++)
            {
                if(order[j]==1)
                {
                    printf("in\n");
                }
                else
                    printf("out\n");
            }
        }
        printf("FINISH\n");
        while(!s.empty())
       {
        s.pop();
       }

    }
    return 0;
}

posted on 2011-07-25 22:04  lonelycatcher  阅读(232)  评论(0编辑  收藏  举报

导航