-
Notifications
You must be signed in to change notification settings - Fork 1
/
37_Evaluate_expressionToTrue_memoization_usingMap.cpp
62 lines (57 loc) · 1.7 KB
/
37_Evaluate_expressionToTrue_memoization_usingMap.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
// Question Link :- https://www.geeksforgeeks.org/problems/boolean-parenthesization5610/1
// Boolean Parenthesization
// (giving TLE)
class Solution{
public:
unordered_map<string, int> mp;
int Solve(string& s, int i, int j, bool isTrue) {
if (i > j) {
return 0;
}
if (i == j) {
if (isTrue) {
return s[i] == 'T';
} else {
return s[i] == 'F';
}
}
string key = to_string(i) + " " + to_string(j) + " " + (isTrue ? "T" : "F");
if (mp.find(key) != mp.end()) {
return mp[key];
}
int ans = 0;
for (int k = i + 1; k <= j-1; k += 2) { // k = k+2
int l_T = Solve(s, i, k - 1, true);
int l_F = Solve(s, i, k - 1, false);
int r_T = Solve(s, k + 1, j, true);
int r_F = Solve(s, k + 1, j, false);
if (s[k] == '|') {
if (isTrue == true) {
ans = (ans + (l_T * r_T)%1003 + (l_T * r_F)%1003 + (l_F * r_T)%1003)%1003;
} else {
ans = (ans + (l_F * r_F)%1003)%1003;
}
}
else if (s[k] == '&') {
if (isTrue == true) {
ans = (ans + (l_T * r_T)%1003)%1003;
} else {
ans = (ans + (l_T * r_F)%1003 + (l_F * r_T)%1003 + (l_F * r_F)%1003)%1003;
}
}
else if (s[k] == '^') {
if (isTrue == true) {
ans = (ans + (l_T * r_F)%1003 + (l_F * r_T)%1003)%1003;
} else {
ans = (ans + (l_T * r_T)%1003 + (l_F * r_F)%1003)%1003;
}
}
}
return mp[key] = (ans%1003);
}
int countWays(int n, string s){
mp.clear();
int ans = Solve(s, 0, n-1, true);
return ans;
}
};