-
Notifications
You must be signed in to change notification settings - Fork 81
/
evaluate_expression_to_true_bottom_up_dp_using_3d_array.cpp
75 lines (65 loc) · 1.46 KB
/
evaluate_expression_to_true_bottom_up_dp_using_3d_array.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <bits/stdc++.h>
using namespace std;
int Solve(string X, int n) {
int dp[n + 1][n + 1];
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= n; j++) {
if (i >= j) {
dp[true][i][j] = (X[i] == 'T');
dp[false][i][j] = (X[i] == 'F');
}
}
}
for (int i = 0; i < n; i += 2) {
for (int j = i; j < n; j += 2) {
if (i == j)
dp[true][i][j] = (X[i] == 'T');
else {
for (int k = i + 1; k <= j; k += 2) {
int l_T = dp[true][i][k - 1];
int r_T = dp[true][k + 1][j];
}
}
}
}
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
for (int k = i + 1; k < j; k += 2) {
int l_T = dp[true][i][k - 1];
int l_F = dp[false][i][k - 1];
int r_T = dp[true][k + 1][j];
int r_F = dp[false][k + 1][j];
if (X[k] == '|') {
if (isTrue == true)
ans += l_T * r_T + l_T * r_F + l_F * r_T;
else
ans += l_F * r_F;
}
else if (X[k] == '&') {
if (isTrue == true)
ans += l_T * r_T;
else
ans += l_T * r_F + l_F * r_T + l_F * r_F;
}
else if (X[k] == '^') {
if (isTrue == true)
ans += l_T * r_F + l_F * r_T;
else
ans += l_T * r_T + l_F * r_F;
}
}
}
}
return dp[1][1][n];
}
signed main() {
#ifndef ONLINE_JUDGE
freopen("../input1.txt", "r", stdin);
freopen("../output1.txt", "w", stdout);
#endif
string X; cin >> X;
memset(dp[0], -1, sizeof(dp[0]));
memset(dp[1], -1, sizeof(dp[1]));
cout << Solve(X, X.length()) << endl;
return 0;
}