Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //fireLUFFY
- //BinarySearch: Stacks->Word_Machine
- ////////Solution//////////
- int solve(vector<string>& ops) {
- stack<int>s;
- for(int i=0;i<ops.size();++i)
- {
- if(s.empty())
- {
- if((ops[i]=="DUP")||(ops[i]=="POP")||(ops[i]=="+")||(ops[i]=="-"))
- return -1;
- else
- s.push(stoi(ops[i]));
- }
- else if(s.size()==1)
- {
- if((ops[i]=="+")||(ops[i]=="-"))
- return -1;
- else
- {
- if(ops[i]=="DUP")
- {
- int val=s.top();
- s.push(val);
- }
- else if(ops[i]=="POP")
- s.pop();
- else
- {
- string c=ops[i];
- int val=stoi(c);
- s.push(val);
- }
- }
- }
- else
- {
- if(ops[i]=="DUP")
- {
- int val=s.top();
- s.push(val);
- }
- else if(ops[i]=="POP")
- s.pop();
- else if(ops[i]=="+")
- {
- int val1=s.top();
- s.pop();
- int val2=s.top();
- s.pop();
- val1=(val1+val2);
- s.push(val1);
- }
- else if(ops[i]=="-")
- {
- int val1=s.top();
- s.pop();
- int val2=s.top();
- s.pop();
- val1=(val1-val2);
- s.push(val1);
- }
- else
- {
- string c=ops[i];
- int val=stoi(c);
- s.push(val);
- }
- }
- }
- return s.top();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement