Description
![](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
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.
这道题目首先要理解它的意思,它是一个类似于栈,但又有点不一样的。
题目意思是:
有两种命令IN,OUT,火车是按照IN的顺序进去的,要你判断能不能以OUT的方式出来;
这里要注意的是不要被样例所给的数据给忽悠了,例如123 321,有些时候并不完完全全是按照顺序输出的。
仔细想想 123 312 ,这组样例,一开始我没明白为什么这个样例是不对的。后来我发现进去肯定是按照123的顺序的,而且3在最上面,1在最下面,所以1是不可能比2先出来的(因为这里只有进去的一条通道,不能再退回到进入的通道中去,这里和栈不一样)
那么 123 213, 这组样例,是可以成功的,命令为“IN IN OUT OUT IN OUT”。
所以这个过程就是模拟每当在栈中进去一个数时,判断已经在栈中的数是不是和OUT命令中的数字相同,若相同,则出栈。
#include<stdio.h>
#include<string.h>
int main(){
int n,top,i,j,k;
char in[12],out[12],s[12],d[12];
while(scanf("%d",&n)!=EOF){
top=0; k=j=0;
//主要是看d[]的数组,若它为0,则表示的是out命令,反之是in命令。
scanf("%s%s",in,out);
for(i=0;i<n;i++){
s[top++]=in[i];
d[k++]=1;
//这里是关键;
while(top>0 && s[top-1]==out[j]) {
top--; j++; d[k++]=0;
}
}
//top表示是否已经全部出栈完毕;
if(top==0){
puts("Yes.");
for(i=0;i<k;i++){
printf("%s\n",d[i]==0 ? "out":"in");
}
}
else puts("No.");
puts("FINISH");
}
}