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.
stack<string>s;voidappend(){stringappended_string;cin>>appended_string;stringtop_string;top_string=s.top();s.push(top_string+appended_string);}voiddelete_k(){intk;cin>>k;stringtop_string;top_string=s.top();stringneeded_string=top_string.substr(0,top_string.size()-k);s.push(needed_string);}voidprint_k(){intk;cin>>k;stringtop_string=s.top();cout<<top_string[k-1]<<endl;}voidundo(){s.pop();}intmain(){/* Enter your code here. Read input from STDIN. Print output to STDOUT */intQ;cin>>Q;s.push("");for(inti=0;i<Q;i++){intops;cin>>ops;switch(ops){case1:append();break;case2:delete_k();break;case3:print_k();break;case4:undo();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 →
My C++ solution. Very easy to understand.