4操作要撤销最近的1操作或者2操作,我们没进行一次1操作或者2操作就插入栈中,遇到4,就弹出栈顶
#include <iostream> #include<stack> using namespace std; stack<string>st; int t; int main() { while(cin>>t){ while(!st.empty())st.pop(); st.push(""); while (t--) { int num1; string s; cin >> num1; if (num1 == 1) { cin >> s; st.push(st.top()+s); } else if (num1 == 2) { int k; cin >> k; string temp = st.top(); temp.erase(temp.size() - k); st.push(temp); } else if (num1 == 3) { int k; cin >> k; //cout << num; string temp = st.top(); cout << temp[k-1] << endl; } else { st.pop(); } } } }