-
Notifications
You must be signed in to change notification settings - Fork 288
/
Copy pathAC_dp_n2.cpp
60 lines (55 loc) · 1.56 KB
/
AC_dp_n2.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
* Author: illuz <iilluzen[at]gmail.com>
* File: AC_dp_n2.cpp
* Create Date: 2015-10-23 14:03:11
* Descripton:
*/
#include <bits/stdc++.h>
using namespace std;
const int N = 110;
class Solution {
private:
vector<int> dp[N][N];
vector<int> helper(string& input, int l, int r) {
if (!dp[l][r].empty())
return dp[l][r];
vector<int> &ans = dp[l][r];
bool isNum = true;
int num = 0;
for (int i = l; i < r; ++i) {
if (!isdigit(input[i])) {
isNum = false;
vector<int> L = helper(input, l, i), R = helper(input, i + 1, r);
for (auto l : L)
for (auto r : R) {
if (input[i] == '+') ans.push_back(l + r);
else if (input[i] == '-') ans.push_back(l - r);
else ans.push_back(l * r);
}
}
if (isNum)
num = num * 10 + (input[i] - '0');
}
if (isNum)
ans.push_back(num);
return ans;
}
public:
vector<int> diffWaysToCompute(string input) {
return helper(input, 0, input.length());
}
};
int main() {
Solution s;
vector<int> ans;
// ans = s.diffWaysToCompute("0+1");
// for (auto i : ans) cout << i << ' ';
// cout << endl;
// ans = s.diffWaysToCompute("2-1-1");
// for (auto i : ans) cout << i << ' ';
// cout << endl;
ans = s.diffWaysToCompute("2*3-4*5");
for (auto i : ans) cout << i << ' ';
cout << endl;
return 0;
}