The editor contains an empty string, S. You must perform operations of the following types:
- append - Append the string W to the end of S.
- delete - Delete the last k characters of S.
- print - Print the kth character of S.
- undo - Undo the last (not previously undone) operation of type 1 or 2, reverting S to the state it was in prior to that operation.
Input:
81 abc3 32 31 xy3 2443 1
Output:
cya
Approach:
C++
#include <bits/stdc++.h>using namespace std;int main(){int q;cin >> q;stack<string> st;string s = "";st.push(s);int p = 0;while (q--){int x;cin >> x;if (x == 1){string str;cin >> str;s.append(str);st.push(s);}else if (x == 2){int k;cin >> k;s.erase(s.size() - k, k);st.push(s);}else if (x == 3){int k;cin >> k;cout << s[k - 1] << "\n";}else if (x == 4){st.pop();s = st.top();}}return 0;}
No comments:
Post a Comment