HDU1022 Train Problem I
栈的应用。。
#include<iostream> #include<stack> using namespace std; const int MAXN=22; int order[MAXN],target[MAXN]; char temp[MAXN]; bool ans[MAXN]; int main() { int n,i; while(cin>>n) { cin>>temp; for(i=0;i<n;i++) order[i]=temp[i]-48; cin>>temp; for(i=0;i<n;i++) target[i]=temp[i]-48; stack<int> s; int A=0,B=0,count=0;; bool ans[MAXN]; bool ok=true; i=0; while(B<n) { if(!s.empty()&&s.top()==target[B]) { s.pop(); B++; ans[i++]=0; } else if(A<n) { s.push(order[A]); A++; ans[i++]=1; } else { ok=false; break; } } if(!ok) cout<<"No.\n"; else { cout<<"Yes.\n"; for(count=i,i=0;i<count;i++) cout<<(ans[i]? "in":"out")<<endl; } cout<<"FINISH\n"; } return 0; }
新 blog : www.hrwhisper.me