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<bits/stdc++.h>#define lli long long intusingnamespacestd;intmain(){ios_base::sync_with_stdio(false);cin.tie(NULL);stack<char>s;stack<string>undo;llitc;cin>>tc;while(tc--){llix;cin>>x;if(x==1){// insertstringv;cin>>v;stringtemp="";if(!undo.empty()){temp=undo.top();}for(inti=0;i<v.length();i++){temp+=v[i];s.push(v[i]);}undo.push(temp);}elseif(x==2){if(s.empty())continue;// deletelliy;cin>>y;while(y--){s.pop();}stringab="";stack<char>temp;temp=s;while(!temp.empty()){ab+=temp.top();temp.pop();}if(ab.length()>0)reverse(ab.begin(),ab.end());undo.push(ab);}elseif(x==3){if(s.empty())continue;// printlliy;cin>>y;stringtemp=undo.top();cout<<temp[y-1]<<"\n";}else{// undoif(!undo.empty()){undo.pop();stringtemp="";if(!undo.empty())temp=undo.top();s=stack<char>();for(autoit:temp){s.push(it);}}}}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 →