Train Problem I(模拟题)

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.

这道题目首先要理解它的意思,它是一个类似于栈,但又有点不一样的。

题目意思是:

有两种命令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");}}

爱情不是避难所,想进去避难的话,是会被赶出来的。

Train Problem I(模拟题)

相关文章:

你感兴趣的文章:

标签云: