TrainProblem I
TimeLimit: 2000/1000 MS (Java/Others) Memory Limit:65536/32768 K (Java/Others)
Total Submission(s): 29991 Accepted Submission(s): 11358
ProblemDescription
As the new term comes, the IgnatiusTrain Station is very busy nowadays. A lot of student want to get back toschool by train(because the trains in the Ignatius Train Station is the fastestall over the world ^v^). But here comes a problem, there is only one railwaywhere all the trains stop. So all the trains come in from one side and get outfrom 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'tleave until train B leaves. The pictures below figure out the problem. Now theproblem for you is, there are at most 9 trains in the station, all the trainshas an ID(numbered from 1 to n), the trains get into the railway in an orderO1, 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. Theinput 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 linecontains "Yes.", and then output your way in exchanging the order(youshould 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.
SampleInput
3 123321
3 123312
SampleOutput
Yes.
in
in
in
out
out
out
FINISH
No.
FINISH
火车进站问题,输入首先包含一个整数N,表示要进站的火车数,然后是依次输入进站火车的序号,然后再输入出站火车的序号,看是否符合出站顺序。
当符合顺序的时候输出”Yes.”依次输入每趟车进站出站的状态进站表示”in” 出战表示”out”,
当不符合顺序的时候输出”No.”程序结束后输出”FINISH”
表示第一次做这样的题目,对照着算法书看了许久。。。
竞赛书上栈部分处理很有意思,不需要明确计数,简单易懂,
#include<stdio.h>
int n;
char input[15],output[15];
int main()
{
while(scanf("%d%s%s",&n,input,output)!=EOF)
{
int stack[15],top=0;
int flag[15];
int i=0,A=0,B=0,ok=1;
while(B<=n)
{
if(input[A]==output[B]) {A++;B++;flag[i++]=1;flag[i++]=0;}
else if(top&&stack[top]==output[B]) {flag[i++]=0;top--;B++;}//出栈
else if(A<=n) {stack[++top]=input[A++];flag[i++]=1;} //进栈
else {ok=0;break;}
}
printf("%s\n",ok?"Yes.":"No.");
if(ok)
{
for(int j=0;j<i-2;j++)
{
printf("%s\n",flag[j]?"in":"out");
}
}
printf("FINISH\n");
}
}