We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
#include<iostream>#include<algorithm>#include<bits/stdc++.h>usingnamespacestd;voidappend(string&s,string&w){s.append(w);}voiddeleteS(string&s,intk){if(s.length()==k)s.erase(s.begin(),s.end());else{k=s.length()-k;s=s.substr(0,k);}}voidprintK(string&s,intk){string::iteratorit;it=s.begin();advance(it,k-1);cout<<*it<<endl;}voidundo(stack<string>&st,string&s){if(st.size()==1){s=st.top();return;}if(!st.empty()){s=st.top();st.pop();}}intmain(){/* Enter your code here. Read input from STDIN. Print output to STDOUT */intq;stack<string>st;strings;cin>>q;while(q--){intx;stringw;cin>>x;if(x==1){cin>>w;st.push(s);append(s,w);}if(x==2){inty;cin>>y;st.push(s);deleteS(s,y);}if(x==3){inty;cin>>y;printK(s,y);}if(x==4){undo(st,s);}}return0;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Simple Text Editor
You are viewing a single comment's thread. Return to all comments →
Simple cpp solution: