Train problem I
Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 24377 Accepted Submission (s): 9196
Problem Descriptionas The new term comes, the Ignatius Train station is very busy nowadays. A lot of student want-get back-to-school by train (because the trains in the Ignatius train station are the fastest all O Ver the world ^v^). But here comes a problem, there are only one railway where all the trains stop. The trains come in from one side and get off 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, tr Ain A can ' t leave until train B leaves. The pictures below figure out the problem. Now the problem for your is, there was at the trains of the station and all the trains have an ID (numbered from 1 to N), the Trains get into the railway in a order O1, your task is to determine whether the trains can get out of an order O2.
Inputthe input contains several test cases. Each test case consists of an integer, the number of trains, and 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.
Outputthe 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 on 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.
Sample INPUT3 123 3213 123 312
Sample OutputYes.inininoutoutoutFINISHNo.FINISH
HintHintfor the first Sample Input, we let train 1 get on, then train 2 and train 3.So now train 3 was at the top of the Railwa Y, 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 the 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 are at the top of the railway at the moment. So we output "No.". Code:
1#include <stdio.h>2#include <cstring>3 using namespacestd;4 intMain ()5 {6 Charo1[Ten],o2[Ten],stack[Ten];7 intN,a=1, b=1, top=0, inout[Ten],flag=0, OK; Use the inout array to hold the stack and the stack, 1 for the pressure, and 0 for the8 while(SCANF ("%d%s %s", &N,O1,O2)! =EOF)9 {Tenmemset (InOut,0,sizeof(InOut)); Oneok=1; AA=0; -b=0; -flag=0; thetop=0; - while(b<=N) - { - if(o1[a]==o2[b])//When the forward stack of the car and the target sequence of the current train is consistent, that is, immediately after the station exit + { -a++; +b++; Ainout[flag++]=2; at } - Else if(top&&stack[top]==o2[b])//In the case of a non-conformance, the top of the train with the target sequence of the current train is consistent, that is, simply outbound - { -top--; -b++; -inout[flag++]=0; in } - Else if(a<=N)//on either of the two cases, just press the stack to { +stack[++top]=o1[a++]; -inout[flag++]=1; the } * else//None of the above three cases, at this time a>n $ {Panax Notoginsengok=0; - Break; the } + A } the if(OK) + { -printf"yes.\n"); $ for(intj=0; j<flag-1; j + +) $ if(InOut [j]==2) -printf"in\nout\n"); - Else if(inout[j]==1) theprintf"in\n"); - Else if(inout[j]==0)Wuyiprintf"out\n"); theprintf"finish\n"); - } Wu Else -printf"no.\nfinish\n"); About } $}
Train problem I