forked from keshavsingh4522/hacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoolean_Parenthesization.cpp
56 lines (50 loc) · 1.22 KB
/
Boolean_Parenthesization.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
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
#include "../debug.h"
int BooleanParenthesization(string &s, int i, int j, bool isTrue){
if(i > j)
return false;
if(i == j){
if(isTrue == true)
return s[i] == 'T';
else
return s[i] == 'F';
}
int ans = 0;
for(int k=i+1; k<=j-1; k+=2){
int lT = BooleanParenthesization(s, i, k-1, true);
int lF = BooleanParenthesization(s, i, k-1, false);
int rT = BooleanParenthesization(s, k+1, j, true);
int rF = BooleanParenthesization(s, k+1, j, false);
if(s[k] == '&'){
if(isTrue == true)
ans += lT*rT;
else
ans += (lT*rF + lF*rT + lF*lF);
}
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 += (lT*rT + lF*rF);
}
}
return ans;
}
int main(){
// freopen("../input.txt", "r", stdin);
freopen("../output.txt", "w", stdout);
freopen("../Error.txt", "w", stderr);
string s = "T^F&T"; // o/p: 3
// s = "T|T&F^T"; // o/p: 4
// s = "T|T&F^T"; // o/p: 4
// s = "T^F|F"; // o/p: 2
cout<<BooleanParenthesization(s, 0, s.size()-1, true);
}