|
| 1 | +/* |
| 2 | +Given a boolean expression with following symbols. |
| 3 | +
|
| 4 | +Symbols |
| 5 | + 'T' ---> true |
| 6 | + 'F' ---> false |
| 7 | +
|
| 8 | +And following operators filled between symbols |
| 9 | +
|
| 10 | +Operators |
| 11 | + & ---> boolean AND |
| 12 | + | ---> boolean OR |
| 13 | + ^ ---> boolean XOR |
| 14 | +
|
| 15 | +Count the number of ways we can parenthesize the expression so that the value of expression evaluates to true. |
| 16 | +
|
| 17 | +For Example: |
| 18 | +The expression is "T | T & F ^ T", it evaluates true |
| 19 | +in 4 ways ((T|T)&(F^T)), (T|(T&(F^T))), (((T|T)&F)^T) |
| 20 | +and (T|((T&F)^T)). |
| 21 | +
|
| 22 | +Return No_of_ways Mod 1003. |
| 23 | +
|
| 24 | +Input: |
| 25 | +First line contains the test cases T. 1<=T<=500 |
| 26 | +Each test case have two lines |
| 27 | +First is length of string N. 1<=N<=100 |
| 28 | +Second line is string S (boolean expression). |
| 29 | +Output: |
| 30 | +No of ways Mod 1003. |
| 31 | +
|
| 32 | +
|
| 33 | +Example: |
| 34 | +Input: |
| 35 | +2 |
| 36 | +7 |
| 37 | +T|T&F^T |
| 38 | +5 |
| 39 | +T^F|F |
| 40 | +
|
| 41 | +Output: |
| 42 | +4 |
| 43 | +2 |
| 44 | +*/ |
| 45 | + |
| 46 | +#include<bits/stdc++.h> |
| 47 | +using namespace std; |
| 48 | + |
| 49 | +int dp[201][201][2]; |
| 50 | + |
| 51 | +int booleanParenthesis(string s, int i, int j, bool isTrue){ |
| 52 | + if(i>j) return 0; |
| 53 | + if(i==j){ |
| 54 | + if(isTrue==true) |
| 55 | + return s[i]=='T'; |
| 56 | + else return s[i]=='F'; |
| 57 | + } |
| 58 | + |
| 59 | + if(dp[i][j][isTrue]!=-1) return dp[i][j][isTrue]; |
| 60 | + |
| 61 | + int ans=0, lf, lt, rf, rt; |
| 62 | + for(int k=i+1;k<j;k+=2){ |
| 63 | + if(dp[i][k-1][true]!=-1) lt=dp[i][k-1][true]; |
| 64 | + else { |
| 65 | + dp[i][k-1][true]=booleanParenthesis(s, i, k-1, true); |
| 66 | + lt=dp[i][k-1][true]; |
| 67 | + } |
| 68 | + if(dp[i][k-1][false]!=-1) lf=dp[i][k-1][false]; |
| 69 | + else { |
| 70 | + dp[i][k-1][false]=booleanParenthesis(s, i, k-1,false); |
| 71 | + lf=dp[i][k-1][false]; |
| 72 | + } |
| 73 | + if(dp[k+1][j][true]!=-1) rt=dp[k+1][j][true]; |
| 74 | + else { |
| 75 | + dp[k+1][j][true]=booleanParenthesis(s, k+1, j, true); |
| 76 | + rt=dp[k+1][j][true]; |
| 77 | + } |
| 78 | + if(dp[k+1][j][false]!=-1) rf=dp[k+1][j][false]; |
| 79 | + else { |
| 80 | + dp[k+1][j][false]=booleanParenthesis(s, k+1, j, false); |
| 81 | + rf=dp[k+1][j][false]; |
| 82 | + } |
| 83 | + |
| 84 | + if(s[k]=='&'){ |
| 85 | + if(isTrue==true) |
| 86 | + ans+=lt*rt; |
| 87 | + else ans+=lt*rf + lf*rt + lf*rf; |
| 88 | + } else if(s[k]=='|'){ |
| 89 | + if(isTrue==true) |
| 90 | + ans+=lt*rt + lt*rf + lf*rt; |
| 91 | + else ans+=lf*rf; |
| 92 | + } else if(s[k]=='^'){ |
| 93 | + if(isTrue==true) |
| 94 | + ans+=lt*rf + lf*rt; |
| 95 | + else ans+=lf*rf + lt*rt; |
| 96 | + } |
| 97 | + } |
| 98 | + return dp[i][j][isTrue]=ans%1003; |
| 99 | +} |
| 100 | + |
| 101 | +int main(){ |
| 102 | + ios_base::sync_with_stdio(false); |
| 103 | + cin.tie(NULL); |
| 104 | + cout.tie(NULL); |
| 105 | + int t; |
| 106 | + cin>>t; |
| 107 | + while(t--){ |
| 108 | + int n; |
| 109 | + cin>>n; |
| 110 | + string s; |
| 111 | + cin>>s; |
| 112 | + memset(dp, -1, sizeof(dp)); |
| 113 | + cout<<booleanParenthesis(s, 0, n-1, true)<<endl; |
| 114 | + } |
| 115 | + |
| 116 | +return 0; |
| 117 | +} |
| 118 | + |
| 119 | + |
| 120 | + |
| 121 | + |
| 122 | +/* |
| 123 | +Alternative Approach |
| 124 | +
|
| 125 | +#include<bits/stdc++.h> |
| 126 | +using namespace std; |
| 127 | +
|
| 128 | +map<string, int> mp; |
| 129 | +
|
| 130 | +int booleanParenthesis(string s, int i, int j, bool isTrue){ |
| 131 | + if(i>j) return 0; |
| 132 | + if(i==j){ |
| 133 | + if(isTrue==true) |
| 134 | + return s[i]=='T'; |
| 135 | + else return s[i]=='F'; |
| 136 | + } |
| 137 | +
|
| 138 | + string tmp=to_string(i); |
| 139 | + tmp.push_back(' '); |
| 140 | + tmp.append(to_string(j)); |
| 141 | + tmp.push_back(' '); |
| 142 | + tmp.append(to_string(isTrue)); |
| 143 | +
|
| 144 | + if(mp.find(tmp)!=mp.end()) return mp[tmp]; |
| 145 | +
|
| 146 | + int ans=0; |
| 147 | + for(int k=i+1;k<j;k+=2){ |
| 148 | + int lt=booleanParenthesis(s, i, k-1, true); |
| 149 | + int lf=booleanParenthesis(s, i, k-1, false); |
| 150 | + int rt=booleanParenthesis(s, k+1, j, true); |
| 151 | + int rf=booleanParenthesis(s, k+1, j, false); |
| 152 | +
|
| 153 | + if(s[k]=='&'){ |
| 154 | + if(isTrue==true) |
| 155 | + ans+=lt*rt; |
| 156 | + else ans+=lt*rf + lf*rt + lf*rf; |
| 157 | + } else if(s[k]=='|'){ |
| 158 | + if(isTrue==true) |
| 159 | + ans+=lt*rt + lt*rf + lf*rt; |
| 160 | + else ans+=lf*rf; |
| 161 | + } else if(s[k]=='^'){ |
| 162 | + if(isTrue==true) |
| 163 | + ans+=lt*rf + lf*rt; |
| 164 | + else ans+=lf*rf + lt*rt; |
| 165 | + } |
| 166 | + } |
| 167 | + return mp[tmp]=ans%1003; |
| 168 | +} |
| 169 | +
|
| 170 | +int main(){ |
| 171 | + ios_base::sync_with_stdio(false); |
| 172 | + cin.tie(NULL); |
| 173 | + cout.tie(NULL); |
| 174 | + int t; |
| 175 | + cin>>t; |
| 176 | + while(t--){ |
| 177 | + int n; |
| 178 | + cin>>n; |
| 179 | + string s; |
| 180 | + cin>>s; |
| 181 | + mp.clear(); |
| 182 | + cout<<booleanParenthesis(s, 0, n-1, true)<<endl; |
| 183 | + } |
| 184 | +
|
| 185 | +return 0; |
| 186 | +} |
| 187 | +*/ |
0 commit comments