HDOJ1022解题报告
Train Problem I
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.
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
Hint 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.".
题意是模拟栈的操作,Chris提醒,以前做过一道数据结构的题目:
元素1,2,3,4依次入栈,不可能的出栈序列有哪些?
解:若后入展的数(4)先出栈,则此前的元素在栈中的顺序是确定的(123),那么关于(1,2,3)的其它排序都是错误的,有
4 1234 4123
那么3先出栈呢?12这样的序列是确定的,只要出栈顺序包括21这一不可能序列,那就是错误的出栈顺序
4 1234 4132
4 1234 4213
4 1234 4231
4 1234 4312
4 1234 3412
4 1234 3124
4 1234 3142
4 1234 3412
4 1234 3124
4 1234 3142
其它要是2先出栈,没有限制,1先出来,那1423就是错的(因为1出,4再出来规定了32这一出栈顺序)。
这个程序可以利用c++的stack库实现。
#include<iostream> #include<string> #include<stack> using namespace std; void main(){ int n; string a,b; int len; while(cin>>n && n!=EOF){ stack<char> s; string arr[100]; cin>>a>>b; int i=0,j=0,k=0; while(i<a.length() || !s.empty()){ if(i<a.length()){ s.push(a[i]); i++; arr[k++] = "in"; } while(!s.empty() && s.top() == b[j]){ s.pop(); arr[k++] = "out"; j++; } if(i==a.length() && j<b.length() && s.top()!=b[j]) break; } if(k==2*a.length()){ cout<<"Yes."<<endl; for(int i=0;i<k;i++) cout<<arr[i]<<endl; cout<<"FINISH"<<endl; }else{ cout<<"No.\nFINISH"<<endl; } } }
基本思路是这样的, 首先,如果进栈序列还未遍历完,那就进一个。然后如果出栈序列索引处元素同栈顶元素,则出栈,直到栈空或者出栈序列遍历完。最后那个if主要控制出栈顺序错误的情况。那就是入栈序列已空而出栈序列当前元素与栈顶元素不匹配(就是进去了出不来的现象)。大while循环判断入栈序列空且栈空(即成功全部入栈出栈的情况)。
最后判断k==2*a.length()作为成功条件,因为k是刚好是入栈序列长+出栈序列长。