HDU 1022 栈的问题

来源:互联网 发布:花千骨网络流行语 编辑:程序博客网 时间:2024/05/17 07:30
5人阅读 评论(0)收藏 举报

Train Problem I

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 21182    Accepted Submission(s): 8015


Problem Description
As the new term comes, the Ignatius Train Station is very busy nowadays. A lot of student want to get back to school by train(because the trains in the Ignatius Train Station is the fastest all over the world ^v^). But here comes a problem, there is only one railway where all the trains stop. So all the trains come in from one side and get out from the other side. For this problem, if train A gets into the railway first, and then train B gets into the railway before train A leaves, train A can't leave until train B leaves. The pictures below figure out the problem. Now the problem for you is, there are at most 9 trains in the station, all the trains has an ID(numbered from 1 to n), the trains get into the railway in an order O1, your task is to determine whether the trains can get out in an order O2.
 

Input
The input contains several test cases. Each test case consists of an integer, the number of trains, and two strings, the order of the trains come in:O1, and the order of the trains leave:O2. The input is terminated by the end of file. More details in the Sample Input.
 

Output
The output contains a string "No." if you can't exchange O2 to O1, or you should output a line contains "Yes.", and then output your way in exchanging the order(you should output "in" for a train getting into the railway, and "out" for a train getting out of the railway). Print a line contains "FINISH" after each test case. More details in the Sample Output.
 

Sample Input
3 123 3213 123 312
 

Sample Output
Yes.inininoutoutoutFINISHNo.FINISH
Hint
Hint
For the first Sample Input, we let train 1 get in, then train 2 and train 3.So now train 3 is at the top of the railway, so train 3 can leave first, then train 2 and train 1.In the second Sample input, we should let train 3 leave first, so we have to let train 1 get in, then train 2 and train 3.Now we can let train 3 leave.But after that we can't let train 1 leave before train 2, because train 2 is at the top of the railway at the moment.So we output "No.".直接是考察栈的输入输出问题。思路:每次先找out序列,然后根据out序列按照in序列进栈。如果栈顶跟out吻合,那么就可以出栈。在每次进栈出栈都要记录是进还是出!
#include <stdio.h>#include <iostream>#include <stack>#include <string.h>#include <string>using namespace std;int main(){    stack<int> s;    string in,out;    int buf[10];    int size1,size2,n;    int pri[25];  //1表示进,2表示出        while(cin>>n)    {        size1=size2=0;        cin>>in>>out;        int flag=0;        if(s.empty()!=true)//清栈            s.pop();        for(int k=0;k<10;++k)            buf[k]=0;        for (int p=0;p<25;++p)            pri[p]=0;                for(int i=0;i<n;++i)  //out        {            if(buf[out[i]-'0']==0)            {                for(int j=0;j<n;++j)  //in  如果没有进栈,那么就得按照顺序一直进栈,直到我们需要的出栈位出现                {                    if(in[j]!=out[i] && buf[in[j]-'0']==0)                    {                        s.push(in[j]-'0');                        pri[size2++]=1;                        buf[in[j]-'0']=1;                                        }                    else                        if(buf[in[j]-'0']==0)                        {                            s.push(in[j]-'0');                            pri[size2++]=1;                            //pri[size2++]=2;                            buf[in[j]-'0']=1;                            break;                        }                }            }            if(buf[out[i]-'0']==1)//说明已经进站了            {                if(s.top()==out[i]-'0')  //栈顶                {                    s.pop();                    pri[size2++]=2;                }                else   //不在栈顶那么顺序不能输出                {                    flag=1;                }                if(flag==1)                    break;            }                    }        if(flag==1)            cout<<"No."<<endl<<"FINISH"<<endl;        else        {            cout<<"Yes."<<endl;            for(int l=0;l<size2;++l)            {                if(pri[l]==1)                    cout<<"in"<<endl;                else                    cout<<"out"<<endl;            }            cout<<"FINISH"<<endl;        }    }    return 0;}


0 0
原创粉丝点击