ACM-HDOJ 1022 简单栈应用
来源:互联网 发布:java面向对象项目 编辑:程序博客网 时间:2024/05/29 18:42
#include <iostream>#include <stack>#include <vector>#include <string>#include <iterator>#include <algorithm>using namespace std;bool begin(unsigned num_of_train,string in_stack, string out_stack, vector<string>& procedure);int main(){ unsigned num_of_train; while(cin >> num_of_train) { string o1, o2; cin >> o1 >> o2; vector<string> procedure; if ( begin(num_of_train, o1, o2, procedure) ) { cout << "Yes." <<endl; copy(procedure.begin(), procedure.end(), ostream_iterator<string>(cout, "\n")); } else cout << "No." <<endl; cout << "FINISH" << endl; }}bool begin(unsigned n, string in, string out, vector<string>& pro) { stack<char> s; unsigned cursor(0); //out's cursor unsigned i(0); while ( i != n ) { s.push(in[i++]); pro.push_back("in"); //匹配的出栈 while(!s.empty()) { if(s.top() == out[cursor]){ ++cursor; s.pop(); pro.push_back("out"); } else break; } } if (s.empty()) return true; else return false;}