Train Problem I
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 12095 Accepted Submission(s): 4435
![](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)
3 123 321 3 123 312
Yes. in in in out out out FINISH No. FINISHFor 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.".HintHint
#include<stdio.h>
#include<stack>
using namespace std;
char in[10];
char out[10];
int flag[20];
int main()
{
int i,j,l,num,n;
while(~scanf("%d",&n)!=0)
{
scanf("%s%s",in,out);
i=j=num=0;
stack<char> s;
while(j<n)
{
if(s.empty()==true||s.top()!=out[j]&&i<n)
{
s.push(in[i]);
flag[num++]=1;
i++;
}
else
{
if(s.top()==out[j])
{
s.pop();
flag[num++]=0;
j++;
}
else {break;}
}
}
if(s.empty()==true)
{
printf("%s\n","Yes.");
for(i=0;i<num;i++)
{
if(flag[i]==1)
{
printf("%s\n","in");
}
else{printf("%s\n","out");}
}
printf("%s\n","FINISH");
}
else
{
printf("%s\n","No.");
printf("%s\n","FINISH");
}
}
}