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.
![](https://i-blog.csdnimg.cn/blog_migrate/e07a7819adc3fce5ee075d011a0a1d3a.jpeg)
![](https://i-blog.csdnimg.cn/blog_migrate/3ed5e24625225c0a75f84ac6dbeceabe.jpeg)
![](https://i-blog.csdnimg.cn/blog_migrate/b72889e34d63c9ba1c9624e8d50888c9.jpeg)
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 321 3 123 312
Sample Output
Yes. in in in out out out FINISH No. 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.".
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.".
Author
解题思路:
火车后进站的先出来,这和栈的特性一样,符合后进先出,题目中给出两个编号串,如第一组数据的123 代表编号1先进站,然后2进站,3进站,出站串321,可以让3先出站,然后2,最后1,可以按出站串那样的顺序完全出站。而第二组数据进站串为123,出站串为312,先看出站串,第一个是3,首先得保证3已经进站,按照进站串的顺序1进站,2进站,3进站,然后3出站,再看出站串的第二个数1,要求1出站,但是此时站里面2是比1后进站的,在2没有出站的情况下1不能出站,因此不能按照出站串那样的顺序出站。
代码:
#include <iostream>
#include <stack>
#include <string.h>
using namespace std;
int n;string in,out;
int flag[25];//用来保存火车的进出站状态
int main()
{
while(cin>>n)
{
cin>>in>>out;
int len=in.length();
stack<char>s;
int c=0;
int j=0;//出站串的第一个火车编号
for(int i=0;i<len;i++)
{
s.push(in[i]);//当前火车入站
flag[c++]=1;//1代表入站
while(!s.empty()&&s.top()==out[j])//当站里面有火车且当栈顶火车编号符合出站的第一个编号时
{
s.pop();//出站
flag[c++]=0;//0代表出站
j++;//出站串的火车编号后移
}
}
if(s.empty())//当入站的火车能够完全按照出站串的顺序出站的时候,意味着栈中的元素为空
{
cout<<"Yes."<<endl;
for(int i=0;i<c;i++)//输出火车进出站的状态
if(flag[i]==1)
cout<<"in"<<endl;
else
cout<<"out"<<endl;
cout<<"FINISH"<<endl;
}
else//不能完全出站
{
cout<<"No."<<endl;
cout<<"FINISH"<<endl;
}
}
return 0;
}