Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdlib.h>
- #include <string>
- #include <stack>
- using namespace std;
- string rev(string s)
- {
- string ans;
- for(int i=s.size()-1; i>=0; --i)
- {
- ans.push_back(s[i]);
- }
- return ans;
- }
- stack<int> st1;
- stack<char> st2;
- int main()
- {
- string s = "M(m(M(18,8),111),m(17,23))";
- for(int i=0; i<s.size(); ++i)
- {
- if(s[i]=='(' || s[i]==')' || s[i]==',')
- {
- s[i]=' ';
- }
- }
- string str;
- bool flag = false;
- for(int i=s.size()-1; i>=0; --i)
- {
- if(s[i]-'0' >=0 && s[i]-'0' <10 )
- {
- flag =true;
- str.push_back(s[i]);
- continue;
- }
- else if(flag)
- {
- flag = false;
- st1.push(atoi( rev(str).c_str() ));
- str.clear();
- }
- if(s[i]=='m' || s[i]=='M') st2.push(s[i]);
- if(st2.size()>0 && st1.size()>1 )
- {
- int a = st1.top();
- st1.pop();
- int b = st1.top();
- st1.pop();
- char c = st2.top();
- st2.pop();
- if(c == 'M')
- st1.push(max(a,b));
- else
- st1.push(min(a,b));
- }
- }
- cout<<st1.top();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement