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
#include<iostream> #include<vector> #include<stack> using namespace std; int main() { int n; string str1,str2; while(cin>>n) { cin>>str1>>str2; vector<string>cmd; stack<int>ls; int cnt[100]; for(int i=0;i<str2.size();i++) cnt[i]=str2[i]-'0'; int t=0; for(int j=0;j<n;j++) { ls.push(str1[j]-'0'); cmd.push_back("in"); while(!ls.empty()&&ls.top()==cnt[t]) { ls.pop(); t++; cmd.push_back("out"); } } if(ls.empty()) { cout<<"Yes."<<endl; for(int i=0;i<cmd.size();i++) cout<<cmd[i]<<endl; } else cout<<"No."<<endl; cout<<"FINISH"<<endl; } return 0; }
#include<iostream> #include<string> #include<vector> #include<stack> using namespace std; int main() { int n; int cnt[100]; string str1,str2; while(cin>>n) { vector<string>cmd; cin.get(); cin>>str1>>str2; stack<int>dict; for(int i=0; i<str2.size(); i++) { cnt[i]=str2[i]-'0'; } int c=0; dict.push(str1[c]-'0'); cmd.push_back("in"); int t=0; while(1) { int x=dict.top(); while(x==cnt[t]) { loop: cmd.push_back("out"); dict.pop(); if(!dict.empty()) { int x=dict.top(); t++; if(x==cnt[t]) { goto loop; } } else { t++; break; } } if(c+1==str1.size()) break; dict.push(str1[++c]-'0'); cmd.push_back("in"); } if(dict.empty()) { cout<<"Yes."<<endl; for(int i=0; i<cmd.size(); i++) cout<<cmd[i]<<endl; } else cout<<"No."<<endl; cout<<"FINISH"<<endl; } return 0; }
原文地址:http://blog.csdn.net/lsgqjh/article/details/46573297