Problem Description
As the new term comes, the Ignatius Train Station is very busy nowadays. A lot of student want to get back to school by train(because the trains in the Ignatius Train Station is the fastest all over the world ^v^). But here comes a problem, there is only one railway where all the trains stop. So all the trains come in 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 into the railway before train A leaves, train A can't leave until train B leaves. The pictures below figure out the problem. Now the problem for you is, there are at most 9 trains in the station, all the trains has an ID(numbered from 1 to n), the trains get into the railway in an order O1, your task is to determine whether the trains can get out in an order O2.
![](http://acm.hdu.edu.cn/data/images/1022-1.jpg)
![](http://acm.hdu.edu.cn/data/images/1022-2.jpg)
![](http://acm.hdu.edu.cn/data/images/1022-3.jpg)
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. The input 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 line contains "Yes.", and then output your way in 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 321 3 123 312
Sample Output
Yes. in in in out out out FINISH No. FINISH
For the first Sample Input, we let train 1 get in, then train 2 and train 3. So now train 3 is at the top of the railway, 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 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 is at the top of the railway at the moment. So we output "No.".
Hint
Hint
Author
Ignatius.L
考察了对栈的应用,好多变量,容易弄混、、、
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 #include <iostream> 2 #include <string.h> 3 using namespace std; 4 #define INF 0x7fffffff 5 6 7 8 int main() 9 { 10 int stack[15], stackHeight; 11 char inTo[15], goOut[15]; 12 int inToIndex, goOutIndex; 13 int order[30], orderIndex;//出栈顺序,0为进栈,1为出栈 14 int trains; 15 while(cin>>trains) 16 { 17 18 memset(stack,0,sizeof(stack)); 19 memset(order,0,sizeof(order)); 20 21 cin>>inTo>>goOut; 22 stack[0] = inTo[0]; 23 order[0]=0; 24 for(inToIndex=stackHeight=orderIndex=0,goOutIndex=0; 25 goOut[goOutIndex]!='\0';goOutIndex++) 26 { 27 // cout<<"inToIndex:"<<inToIndex<<" trains:"<<trains<<endl; 28 while(inToIndex<trains)//火车必须全部进站,当进站的火车数量大于火车时,所给条件不成立 29 { 30 if(goOut[goOutIndex]==stack[stackHeight]) 31 { 32 // cout<<"stack:"<<stack[stackHeight]-48<<endl; 33 // cout<<"order:"<<order[orderIndex]<<"orderIndex:"<<orderIndex<<endl; 34 stackHeight--; 35 order[++orderIndex]=1; 36 37 break; 38 } 39 40 ++stackHeight; 41 ++inToIndex; 42 // cout<<"stackHeight:"<<stackHeight<<endl; 43 stack[stackHeight]=inTo[inToIndex]; 44 order[++orderIndex]=0; 45 // cout<<"inTo"<<inTo[inToIndex]<<endl; 46 // cout<<"stackHeight:"<<stackHeight<<endl; 47 // cout<<"stack:"<<stack[stackHeight]-48<<endl; 48 } 49 50 } 51 52 // for(int i=0; i<=orderIndex; i++) 53 // { 54 // cout<<order[i]<<endl; 55 // } 56 57 trains=(trains<<1);//乘以2 58 // cout<<trains<<endl; 59 if(orderIndex == trains-1) 60 { 61 cout<<"Yes."<<endl; 62 for(int i=0; i<=orderIndex; i++) 63 { 64 if(order[i]==0) 65 cout<<"in"<<endl; 66 else 67 cout<<"out"<<endl; 68 } 69 cout<<"FINISH"<<endl; 70 } 71 else 72 { 73 cout<<"No."<<endl<<"FINISH"<<endl; 74 } 75 } 76 77 78 return 0; 79 }