1)模拟题,不要钻牛角尖,第一种模拟思路过不了,就换一个思路再写;另外,每一次WA,都要检查一遍格式准确吗。
#include <iostream> #include <stack> #include <string.h> using namespace std; int main() { stack <int> z1; int n; string num1,num2; int IN[120],EX[120]; int jilu[320]; while(cin>>n){ cin>>num1>>num2; while(!z1.empty()){ z1.pop(); } memset(IN,0,sizeof(IN)); memset(EX,0,sizeof(EX)); memset(jilu,0,sizeof(jilu)); for(int i=0;i<n;i++){ IN[i]=num1[i]-'0'; } for(int i=0;i<n;i++){ EX[i]=num2[i]-'0'; } int i,j,k; i=j=k=0;//k 1为进,2为出 while(i<n){ z1.push(IN[i++]); jilu[k++]=1; while(!z1.empty()&&EX[j]==z1.top()){ j++; z1.pop(); jilu[k++]=2; } } if(z1.empty()){ cout<<"Yes."<<endl; for(int i=0;i<k;i++){ if(jilu[i]==1){ cout<<"in"<<endl; } else if(jilu[i]==2){ cout<<"out"<<endl; } } cout<<"FINISH"<<endl; } else { cout<<"No."<<endl; cout<<"FINISH"<<endl; } } }
2)
3 123 321 3 123 312
Yes. in in in out out out FINISH No. FINISHFor 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.".HintHint