栈的练习 Train Problem I

来源:互联网 发布:java queue 编辑:程序博客网 时间:2024/05/16 08:29

Train Problem I

TimeLimit: 2000/1000 MS(Java/Others)    MemoryLimit: 65536/32768 K (Java/Others)
Total Submission(s):6794    AcceptedSubmission(s): 2486


Problem Description
As the new term comes, the Ignatius Train Station is very busynowadays. A lot of student want to get back to school bytrain(because the trains in the Ignatius Train Station is thefastest all over the world ^v^). But here comes a problem, there isonly one railway where all the trains stop. So all the trains comein 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 intothe railway before train A leaves, train A can't leave until trainB leaves. The pictures below figure out the problem. Now theproblem for you is, there are at most 9 trains in the station, allthe trains has an ID(numbered from 1 to n), the trains get into therailway in an order O1, your task is to determine whether thetrains can get out in an order O2.
栈的练习 <wbr>Train <wbr>Problem <wbr>I栈的练习 <wbr>Train <wbr>Problem <wbr>I栈的练习 <wbr>Train <wbr>Problem <wbr>I
 

Input
The input contains several test cases. Each test case consists ofan integer, the number of trains, and two strings, the order of thetrains come in:O1, and the order of the trains leave:O2. The inputis terminated by the end of file. More details in the SampleInput.
 

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 yourway in exchanging the order(you should output "in" for a traingetting into the railway, and "out" for a train getting out of therailway). Print a line contains "FINISH" after each test case. Moredetails in the Sample Output.
 

Sample Input
3 123321 
3 123312
 

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 andtrain 3. So now train 3 is at the top of the railway, so train 3can leave first, then train 2 and train 1. In the second Sampleinput, we should let train 3 leave first, so we have to let train 1get in, then train 2 and train 3. Now we can let train 3 leave. Butafter that we can't let train 1 leave before train 2, because train2 is at the top of the railway at the moment. So we output"No.".
 
#include<stdio.h>
#include<string.h>
intnum1[11],num2[11],s[22],l[11];
intmain()
{
   int i,j,n,cnt,k;
   while(scanf("%d",&n)!=EOF)
   {
       k=0;
      memset(s,0,sizeof(s));
      for(i=0;i<n;i++)
          scanf("",&num1[i]);
      for(i=0;i<n;i++)
          scanf("",&num2[i]);
       j=0;
       cnt=i=k=-1;
       l[++k]=num1[++i];
       ++cnt;
      while(i<n&&j<n)
       {
          while(k>=0&&l[k]==num2[j])
          {
             s[++cnt]=1;
             --k;
             ++j;
          }
        ++cnt;
        l[++k]=num1[++i];
       }
      if(i==n&&j==n)
       {
          puts("Yes.");
          for(i=0;i<cnt;i++)
             puts(s[i]?"out":"in");
       }
       else
       {
          puts("No.");
       }
        puts( "FINISH");
   }
   return 0;
}

原创粉丝点击