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<cmath>#include<cstdio>#include<vector>#include<iostream>#include<sstream>#include<algorithm>#include<stack>usingnamespacestd;intmain(){/* Enter your code here. Read input from STDIN. Print output to STDOUT */vector<string>operations;stack<pair<int,string>>undo;intnumOfOps;cin>>numOfOps;cin.ignore();stringstr="";for(inti=0;i<numOfOps;++i){stringop;getline(cin,op);intcode;stringarg;stringstreamss(op);ss>>code>>arg;switch(code){case1:str+=arg;undo.push({1,arg});break;case2:undo.push({2,str.substr(str.size()-stoi(arg))});str.erase(str.size()-stoi(arg));break;case3:cout<<str[stoi(arg)-1]<<endl;break;case4:auto[lastOp,lastItem]=undo.top();switch(lastOp){case1:str.erase(str.size()-lastItem.size());break;case2:str+=lastItem;break;}undo.pop();break;}}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 →
C++20 solution: