-
Notifications
You must be signed in to change notification settings - Fork 0
/
#163.cpp
40 lines (32 loc) · 972 Bytes
/
#163.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include<iostream>
#include<string>
#include<unordered_set>
#include<cstdlib>
using namespace std;
pair<int, string> recurse(string s, unordered_set<char>& ops) {
char last = s[s.length()-1];
string remaining = s.substr(0, s.length()-1);
if (ops.find(last) != ops.end()) {
pair<int, string> p1 = recurse(remaining, ops);
pair<int, string> p2 = recurse(p1.second, ops);
if (last == '+') {
return make_pair(p2.first + p1.first, p2.second);
} else if (last == '-') {
return make_pair(p2.first - p1.first, p2.second);
} else if (last == '/') {
return make_pair(p2.first / p1.first, p2.second);
} else if (last == '*') {
return make_pair(p2.first * p1.first, p2.second);
}
}
return make_pair((int)last - 48, remaining);
}
int compute(string s) {
unordered_set<char> ops({'-', '+', '*', '/'});
pair<int, string> result = recurse(s, ops);
return result.first;
}
int main() {
string s = "5711+-/3*211++-";
cout << compute(s) << endl;
}