Address: http://acm.hdu.edu.cn/showproblem.php?pid=1022 title: 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.".
Idea: Purple book on the original problem, test instructions is to give you a pit stop before the sequence, judge whether these numbers can be similar into the stack out of the operation of the stack to get the number of questions given to the series;
It's good to simulate with a stack.
PS: Be sure to pay attention to the output format!!! After writing the code, Leng has been WA, started because the stack did not empty, and then found that the output format is not correct!!!
The lesson of blood, changed long time!!!
1#include <iostream>2#include <algorithm>3#include <cstdio>4#include <cmath>5#include <cstring>6#include <queue>7#include <stack>8#include <map>9#include <vector>Ten#include <string> One A #definePI ACOs ((double)-1) - #defineE exp (double (1)) - #defineN 1000000 the using namespacestd; - - intMain (void) - { + intt,a,b,flag,c; - stringS,e,code; +stack<int>SK; A while(SCANF ("%d", &t) = =1&&t) at { -Cin>>s>>e; -flag=1; -A=b=c=0; - while(b<t) - { in if(S[a] = =E[b]) - { toa++;b++; +code[c++]=1; -code[c++]=0; the } * Else if(!sk.empty () && e[b] = =sk.top ()) $ {Panax NotoginsengSk.pop (); b++; -code[c++]=0; the } + Else if(A <t) A { theSk.push (s[a++]); +code[c++]=1; - } $ Else $ { -Flag =0; - Break; the } - }Wuyi if(flag) the { -cout<<"Yes."<<Endl; Wu for(inti =0;i<2*t;i++) - if(Code[i]) Aboutcout<<"inch"<<Endl; $ Else -cout<<" out"<<Endl; - } - Else Acout<<"No."<<Endl; +cout<<"FINISH"<<Endl; the while(!sk.empty ()) - Sk.pop (); $ } the return 0; the}
View Code
Hangzhou Electric 1022Train Problem I