Sample Input
3 123 321 3 123 312
Sample Output
Yes. in in in out out out FINISH No. FINISH 考察栈的应用,给定序列str1和str2,问你能否在str1的基础上只进行入栈 出栈得到str2 用stl水就可以了 但是我水的很艰难啊各种WA 直接上代码吧:
#include <cstdio> #include <vector> #include <stack> #include <string> #include <iostream> #include <algorithm> #define MAXN 10010 #define ll long long using namespace std; vector<string> ans; int main(void) { int n; string str1, str2; stack<char> st; while(scanf("%d", &n) != EOF) { ans.clear(); while(!st.empty())//用栈的时候千万记得清空栈 st.pop(); cin >> str1 >> str2; int len = str1.size(); int i = 0; int j = 0; while(i<len) { st.push(str1[i++]); ans.push_back("in"); while(!st.empty() && st.top() == str2[j]) {//每次要一直退栈到不能退为止 st.pop(); ans.push_back("out"); ++j; } } while(j < len) { if(st.top() == str2[j]) { st.pop(); ans.push_back("out"); } ++j; } if(st.empty()) { cout << "Yes." << endl; for(int i=0; i<ans.size(); ++i) { cout << ans[i] << endl; } } else cout << "No." << endl; cout << "FINISH" << endl; } return 0; }