-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboolean_evaluation.cpp
213 lines (202 loc) · 7.96 KB
/
boolean_evaluation.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include <iostream>
#include <string_view>
#include <chrono>
#include <unordered_map>
static std::pair<std::size_t, std::size_t> evaluate(const std::string_view expression)
{
if(expression.size() == 0)
return { 0, 0 };
if(expression.size() == 1)
{
if(expression[0] == '0')
return { 0, 1 };
else return { 1, 0 };
}
std::size_t numWaysTrue = 0;
std::size_t numWaysFalse = 0;
// Build the expression tree for each operator as root (of a sub-tree)
for(std::size_t i = 0; const auto& ch : expression)
{
if(ch != '1' && ch != '0')
{
// Build the left expression tree
auto leftResult = evaluate(expression.substr(0, i));
// Build the right expression tree
auto rightResult = evaluate(expression.substr(i + 1));
switch(ch)
{
case '|':
// The number of ways of making 'a | b' true =
// number of ways of making 'a' true * number of ways of making 'b' true
// + number of ways of making 'a' true * number of ways of making 'b' false
// + number of ways of making 'b' true * number of ways of making 'a' false
numWaysTrue += leftResult.first * rightResult.first
+ leftResult.first * rightResult.second + rightResult.first * leftResult.second;
// The number of ways of making 'a | b' false =
// number of ways of making 'b' false * number of ways of making 'b' false
numWaysFalse += leftResult.second * rightResult.second;
break;
case '&':
// The number of ways of making 'a & b' true =
// number of ways of making 'a' true * number of ways of making 'b' true
numWaysTrue += leftResult.first * rightResult.first;
// The number of ways of making 'a & b' false =
// number of ways of making 'a' false * number of ways of making 'b' false
// + number of ways of making 'b' false * number of ways of making 'a' true
// + number of ways of making 'a' false * number of ways of making 'b' true
numWaysFalse += leftResult.second * rightResult.second
+ leftResult.second * rightResult.first + rightResult.second * leftResult.first;
break;
case '^':
// The number of ways of making 'a ^ b' true =
// number of ways of making 'a' true * number of ways of making 'b' false
// + number of ways of making 'b' true * number of ways of making 'a' false
// The number of ways of making 'a ^ b' false =
// number of ways of making 'a' true * number of ways of making 'b' true
// + number of ways of making 'b' false * number of ways of making 'b' false
numWaysTrue += leftResult.first * rightResult.second + rightResult.first * leftResult.second;
numWaysFalse += leftResult.first * rightResult.first + leftResult.second * rightResult.second;
break;
}
}
++i;
}
return { numWaysTrue, numWaysFalse };
}
template<typename hasher = std::hash<std::string_view>>
static std::pair<std::size_t, std::size_t> evaluateMemo(const std::string_view expression,
std::unordered_map<std::string_view, std::pair<std::size_t, std::size_t>, hasher>& memo)
{
if(expression.size() == 0)
return { 0, 0 };
if(expression.size() == 1)
{
if(expression[0] == '0')
return { 0, 1 };
else return { 1, 0 };
}
if(auto it = memo.find(expression); it != memo.end())
return it->second;
std::size_t numWaysTrue = 0;
std::size_t numWaysFalse = 0;
// Build the expression tree for each operator as root (of a sub-tree)
for(std::size_t i = 0; const auto& ch : expression)
{
if(ch != '1' && ch != '0')
{
// Build the left expression tree
auto leftResult = evaluateMemo<hasher>(expression.substr(0, i), memo);
// Build the right expression tree
auto rightResult = evaluateMemo<hasher>(expression.substr(i + 1), memo);
switch(ch)
{
case '|':
// The number of ways of making 'a | b' true =
// number of ways of making 'a' true * number of ways of making 'b' true
// + number of ways of making 'a' true * number of ways of making 'b' false
// + number of ways of making 'b' true * number of ways of making 'a' false
numWaysTrue += leftResult.first * rightResult.first
+ leftResult.first * rightResult.second + rightResult.first * leftResult.second;
// The number of ways of making 'a | b' false =
// number of ways of making 'b' false * number of ways of making 'b' false
numWaysFalse += leftResult.second * rightResult.second;
break;
case '&':
// The number of ways of making 'a & b' true =
// number of ways of making 'a' true * number of ways of making 'b' true
numWaysTrue += leftResult.first * rightResult.first;
// The number of ways of making 'a & b' false =
// number of ways of making 'a' false * number of ways of making 'b' false
// + number of ways of making 'b' false * number of ways of making 'a' true
// + number of ways of making 'a' false * number of ways of making 'b' true
numWaysFalse += leftResult.second * rightResult.second
+ leftResult.second * rightResult.first + rightResult.second * leftResult.first;
break;
case '^':
// The number of ways of making 'a ^ b' true =
// number of ways of making 'a' true * number of ways of making 'b' false
// + number of ways of making 'b' true * number of ways of making 'a' false
// The number of ways of making 'a ^ b' false =
// number of ways of making 'a' true * number of ways of making 'b' true
// + number of ways of making 'b' false * number of ways of making 'b' false
numWaysTrue += leftResult.first * rightResult.second + rightResult.first * leftResult.second;
numWaysFalse += leftResult.first * rightResult.first + leftResult.second * rightResult.second;
break;
}
}
++i;
}
std::pair<std::size_t, std::size_t> result = { numWaysTrue, numWaysFalse };
memo.insert({ expression, result });
return result;
}
template<typename hasher = std::hash<std::string_view>>
static std::pair<std::size_t, std::size_t> evaluateMemo(const std::string_view expression)
{
std::unordered_map<std::string_view, std::pair<std::size_t, std::size_t>, hasher> memo;
return evaluateMemo<hasher>(expression, memo);
}
struct Solution1
{
auto operator()(const std::string_view expression)
{
return evaluate(expression);
}
};
struct Solution2
{
auto operator()(const std::string_view expression)
{
return evaluateMemo<>(expression);
}
};
// For this optimization to work correctly in general case , all string_view(s) which are identical by value must have identical base address (pointer returned by data() method).
// For memoization purpose, this would still work but may lead to inconsistent performance results if there are many string_view(s) identical by value but different by their base addresses (data()).
struct SVEfficientHash
{
std::size_t operator()(const std::string_view str) const
{
auto h1 = std::hash<std::size_t>{}(str.size());
auto h2 = std::hash<const char*>{}(str.data());
return h1 ^ h2;
}
};
struct Solution3
{
auto operator()(const std::string_view expression)
{
return evaluateMemo<SVEfficientHash>(expression);
}
};
template<typename Sol>
static void runNumWays(const std::string_view expression)
{
auto start = std::chrono::steady_clock::now();
auto result = Sol { } (expression);
auto end = std::chrono::steady_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::duration<float, std::milli>>(end - start).count();
std::cout << "Time taken: " << elapsed << " ms\n";
std::cout << "Num ways of true = " << result.first << "\n";
std::cout << "Num ways of false = " << result.second << "\n";
}
static void run(const std::string_view expression)
{
static std::size_t runCount = 0;
std::cout << "------------RUN: " << runCount << " ------------\n";
++runCount;
std::cout << "INPUT: " << expression << "\n";
std::cout << "**Solution no 1**\n";
runNumWays<Solution1>(expression);
std::cout << "**Solution no 2**\n";
runNumWays<Solution2>(expression);
std::cout << "**Solution no 3**\n";
runNumWays<Solution3>(expression);
}
int main()
{
run( "0&0&0&1^1|0");
run( "0&0&0&1^1|0|0^1&1&0|1^0^0^0^0|1&1");
run( "0&0&0&1^1|0");
run("1^0|0|1");
return 0;
}