-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoolean Parenthesization
More file actions
74 lines (59 loc) Β· 2.1 KB
/
Copy pathBoolean Parenthesization
File metadata and controls
74 lines (59 loc) Β· 2.1 KB
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
class Solution{
public:
int mod= 1003;
int solve(int i, int j, int isTrue, string &s, vector<vector<vector<int>>> &dp){
if(i>j) return 0;
if(dp[i][j][isTrue] != -1) return dp[i][j][isTrue];
if(i==j){
if(isTrue){
if(s[i]=='T') return 1;
else{
return 0;
}
}
else{
if(s[i]=='F') return 1;
else{
return 0;
}
}
}
int ways=0;
for(int index=i+1; index<j; index+=2){
int lt= solve(i,index-1,1,s,dp);
int lf= solve(i,index-1,0,s,dp);
int rt= solve(index+1,j,1,s,dp);
int rf= solve(index+1,j,0,s,dp);
if(s[index]=='&'){
if(isTrue){
ways= (ways%mod + (lt % mod * rt % mod) % mod ) % mod;
}
else{
ways= (ways%mod + (lf % mod * rt % mod)%mod + (lt % mod * rf % mod)%mod + (lf % mod * rf % mod)%mod ) % mod;
}
}
else if(s[index]=='|'){
if(isTrue){
ways= (ways%mod + (lf % mod * rt % mod)%mod + (lt % mod * rf % mod)%mod + (lt % mod * rt % mod)% mod )%mod;
}
else{
ways=(ways%mod + (lf % mod * rf % mod)% mod) % mod;
}
}
else{
if(isTrue){
ways= (ways%mod + (lf % mod * rt % mod)%mod + (lt % mod * rf % mod)% mod) % mod;
}
else{
ways=(ways%mod + (lt % mod * rt % mod)%mod + (lf % mod * rf % mod)%mod) % mod;
}
}
}
dp[i][j][isTrue]= ways % mod;
return dp[i][j][isTrue];
}
int countWays(int N, string S){
vector<vector<vector<int>>>dp(N+1, vector<vector<int>>(N+1, vector<int>(3,-1)));
return solve(0,N-1,1,S,dp);
}
};