forked from sureshmangs/Code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboolean_patenthesization_memoization.cpp
187 lines (156 loc) · 3.99 KB
/
boolean_patenthesization_memoization.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
Given a boolean expression with following symbols.
Symbols
'T' ---> true
'F' ---> false
And following operators filled between symbols
Operators
& ---> boolean AND
| ---> boolean OR
^ ---> boolean XOR
Count the number of ways we can parenthesize the expression so that the value of expression evaluates to true.
For Example:
The expression is "T | T & F ^ T", it evaluates true
in 4 ways ((T|T)&(F^T)), (T|(T&(F^T))), (((T|T)&F)^T)
and (T|((T&F)^T)).
Return No_of_ways Mod 1003.
Input:
First line contains the test cases T. 1<=T<=500
Each test case have two lines
First is length of string N. 1<=N<=100
Second line is string S (boolean expression).
Output:
No of ways Mod 1003.
Example:
Input:
2
7
T|T&F^T
5
T^F|F
Output:
4
2
*/
#include<bits/stdc++.h>
using namespace std;
int dp[201][201][2];
int booleanParenthesis(string s, int i, int j, bool isTrue){
if(i>j) return 0;
if(i==j){
if(isTrue==true)
return s[i]=='T';
else return s[i]=='F';
}
if(dp[i][j][isTrue]!=-1) return dp[i][j][isTrue];
int ans=0, lf, lt, rf, rt;
for(int k=i+1;k<j;k+=2){
if(dp[i][k-1][true]!=-1) lt=dp[i][k-1][true];
else {
dp[i][k-1][true]=booleanParenthesis(s, i, k-1, true);
lt=dp[i][k-1][true];
}
if(dp[i][k-1][false]!=-1) lf=dp[i][k-1][false];
else {
dp[i][k-1][false]=booleanParenthesis(s, i, k-1,false);
lf=dp[i][k-1][false];
}
if(dp[k+1][j][true]!=-1) rt=dp[k+1][j][true];
else {
dp[k+1][j][true]=booleanParenthesis(s, k+1, j, true);
rt=dp[k+1][j][true];
}
if(dp[k+1][j][false]!=-1) rf=dp[k+1][j][false];
else {
dp[k+1][j][false]=booleanParenthesis(s, k+1, j, false);
rf=dp[k+1][j][false];
}
if(s[k]=='&'){
if(isTrue==true)
ans+=lt*rt;
else ans+=lt*rf + lf*rt + lf*rf;
} else if(s[k]=='|'){
if(isTrue==true)
ans+=lt*rt + lt*rf + lf*rt;
else ans+=lf*rf;
} else if(s[k]=='^'){
if(isTrue==true)
ans+=lt*rf + lf*rt;
else ans+=lf*rf + lt*rt;
}
}
return dp[i][j][isTrue]=ans%1003;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t;
cin>>t;
while(t--){
int n;
cin>>n;
string s;
cin>>s;
memset(dp, -1, sizeof(dp));
cout<<booleanParenthesis(s, 0, n-1, true)<<endl;
}
return 0;
}
/*
Alternative Approach
#include<bits/stdc++.h>
using namespace std;
map<string, int> mp;
int booleanParenthesis(string s, int i, int j, bool isTrue){
if(i>j) return 0;
if(i==j){
if(isTrue==true)
return s[i]=='T';
else return s[i]=='F';
}
string tmp=to_string(i);
tmp.push_back(' ');
tmp.append(to_string(j));
tmp.push_back(' ');
tmp.append(to_string(isTrue));
if(mp.find(tmp)!=mp.end()) return mp[tmp];
int ans=0;
for(int k=i+1;k<j;k+=2){
int lt=booleanParenthesis(s, i, k-1, true);
int lf=booleanParenthesis(s, i, k-1, false);
int rt=booleanParenthesis(s, k+1, j, true);
int rf=booleanParenthesis(s, k+1, j, false);
if(s[k]=='&'){
if(isTrue==true)
ans+=lt*rt;
else ans+=lt*rf + lf*rt + lf*rf;
} else if(s[k]=='|'){
if(isTrue==true)
ans+=lt*rt + lt*rf + lf*rt;
else ans+=lf*rf;
} else if(s[k]=='^'){
if(isTrue==true)
ans+=lt*rf + lf*rt;
else ans+=lf*rf + lt*rt;
}
}
return mp[tmp]=ans%1003;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t;
cin>>t;
while(t--){
int n;
cin>>n;
string s;
cin>>s;
mp.clear();
cout<<booleanParenthesis(s, 0, n-1, true)<<endl;
}
return 0;
}
*/