-
Notifications
You must be signed in to change notification settings - Fork 1
/
boolean parenthisation.cpp
87 lines (85 loc) · 2.09 KB
/
boolean parenthisation.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
///problem link:-https://practice.geeksforgeeks.org/problems/boolean-parenthesization/0
using namespace std;
//3D matrix for memoization as 3 variables change during recursion
//calls i, j and istrue
long long int dp[501][501][3];
int solve(string str,int i,int j,char istrue)
{
if(i>j)
return 0;
if(i==j)
{
if(istrue=='T')
return str[i]=='T';
else
return str[i]=='F';
}
if(dp[i][j][istrue]!=-1)
return dp[i][j][istrue];
long long int ans=0,lt,lf,rt,rf,k;
//k+2 as k is always partitioned or evaluated on a operator
//k=i+1 as if the expression is T^F|F then k can only
//start from i+1 i.e. 1 i.e. we can but brackets like (T)^(F|F)
//but not ()T(^F|F)
for(k=i+1;k<=j-1;k+=2)
{
if(dp[i][k-1]['T']!=-1)
lt=dp[i][k-1]['T'];
else
lt=solve(str,i,k-1,'T');
if(dp[i][k-1]['F']!=-1)
lf=dp[i][k-1]['F'];
else
{
lf=solve(str,i,k-1,'F');
}
if(dp[k+1][j]['T']!=-1)
rt=dp[k+1][j]['T'];
else
rt=solve(str,k+1,j,'T');
if(dp[k+1][j]['F']!=-1)
rf=dp[k+1][j]['F'];
else
rf=solve(str,k+1,j,'F');
if(str[k]=='&')
{
if(istrue=='T')
ans+=lt*rt;
else
ans+=lt*rf+lf*rt+lf*rf;
}
else if(str[k]=='|')
{
if(istrue=='T')
ans+=lt*rt+lt*rf+lf*rt;
else
ans+=lf*rf;
}
else if(str[k]=='^')
{
if(istrue=='T')
ans+=lt*rf+lf*rt;
else
ans+=lt*rt+lf*rf;
}
}
dp[i][j][istrue]=ans%1003;
return ans%1003;
}
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
string str;
cin>>str;
memset(dp,-1,sizeof(dp));
//mp.clear();
cout<<solve(str,0,n-1,'T')<<endl;
}
//code
return 0;
}