Train problem I
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 Input
3 123 3213) 123 312
Sample Output
Yes.inininoutoutoutFINISHNo.FINISHHintHintfor the first Sample Input, we let train 1 get in, then train 2 and Train 3.So now train 3 are at the top of the railway, so train 3 can leave first, then train 2 and train 1.In the SEC Ond Sample input, we should let train 3 leave first, so we had to let 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.".
Stack application: Given 1 out of the stack sequence, see whether the current into the stack sequence can be reached by the stack to reach the stack sequence. The code is as follows:
#include <cstdio> #include <stack>using namespace Std;int main () { int n; Char F1[1010],f2[1010];//f1 Enter the stack order, F2 record the stack order while (scanf ("%d%s%s", &n,f1,f2)!=eof) { int g[1010],i=0,j=0 , k=0;//g[] Record access situation stack<char> T; while (i<n) { if (t.empty () | | | T.top ()!=f2[i]&&j<n) { t.push (f1[j++]); G[K++]=1;//1 is in } else if (T.top () ==f2[i]) { t.pop (); g[k++]=0;//0 for Out i++; } else break; } if (T.empty ()) {//Stack t is empty, you can generate the current out-stack sequence printf ("yes.\n"); for (int i=0;i<k;i++) { if (G[i]) printf ("in\n"); else printf ("out\n"); } printf ("finish\n"); } else printf ("no.\nfinish\n"); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU 1022.Train problem I "Stack application" "August 19"