codepad
[
create a new paste
]
login
|
about
Language:
C
C++
D
Haskell
Lua
OCaml
PHP
Perl
Plain Text
Python
Ruby
Scheme
Tcl
#include <stack> #include <string> #include <iostream> using namespace std; stack<char> reverseStack(const stack<char>& s){ stack<char> res; stack<char> src(s); while(!src.empty()){ res.push( src.top() ); src.pop(); } return res; } void printStack(const std::stack<char>& stk){ stack<char> s(stk); while(!s.empty()){ cout << s.top() << endl; s.pop(); } } stack<char> processLine(const stack<char> &stk){ stack<char> src(reverseStack(stk)); stack<char> resultStack; char prev = -1; while(! src.empty()){ char top = src.top(); //if no sign of backspace just add it if(top != '-' && top != '<' && prev != '<'){ resultStack.push(top); } //else its either '-' or '<', so do proper checking else{ //check if the 2 sequenced character created a backspace? if(prev == '<' && top == '-'){ //backspace detected, so pop top if any if(!resultStack.empty()) resultStack.pop(); } prev = top; //update previous } src.pop(); } return reverseStack(resultStack); } int main(){ std::string str = "Nn<-<-<-<-No <-w iz<-sthe<-<-<- the time."; std::stack<char> stk; for(unsigned i = 0; i < str.size(); ++i){ stk.push(str[i]); } printStack(processLine(stk)); return 0; }
Private
[
?
]
Run code
Submit