• + 0 comments

    Simple cpp solution:

    #include <iostream>
    #include <algorithm>
    #include<bits/stdc++.h>
    using namespace std;
    
    void append(string &s, string &w){
        s.append(w);
    }
    
    void deleteS(string &s, int k){
        if(s.length() == k)
            s.erase(s.begin(),s.end());
        else{
           k = s.length()-k;
           s = s.substr(0,k); 
        }    
    }
    
    void printK(string &s, int k){
        string :: iterator it;
        it = s.begin();
        advance(it,k-1);
        cout<<*it<<endl;
    }
    
    void undo(stack<string> &st, string &s){
        if(st.size() == 1){
            s = st.top();
            return;
        }
        if(!st.empty()){
            s = st.top();
            st.pop();
        }
    }
    
    int main() {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT */  
        int q;
        stack<string> st;
        string s;
        cin>>q;
        while(q--){
            int x;
            string w;
            cin>>x;
            if(x==1){
                cin>>w;
                st.push(s);
                append(s,w);                        
            }
            if(x==2){
                int y;
                cin>>y;
                st.push(s);
                deleteS(s,y);            
            }
            if(x==3){
                int y;
                cin>>y;            
                printK(s,y);
            }
            if(x==4){
                undo(st,s);
            }
        } 
        return 0;
    }