-
Notifications
You must be signed in to change notification settings - Fork 12
/
1324-Equivalent_Boolean_Expressions.cpp
69 lines (61 loc) · 1.09 KB
/
1324-Equivalent_Boolean_Expressions.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
#include <iostream>
#include <algorithm>
using namespace std;
int N, M;
string S;
int eval() {
if ('a' <= S[N] and S[N] <= 'j') {
int p = N++; // a
return (M>>(S[p] - 'a'))&1;
}
if (S[N] == '!') {
++N; // !
return 1 - eval();
}
// S[N] == '('
++N; // (
int tmp = eval();
int res = 0;
while (S[N] != ')') {
if (S[N] == '|') {
++N; // |
res |= tmp;
tmp = eval();
}
else { // S[N] == '&'
++N; // &
tmp &= eval();
}
}
++N; // )
res |= tmp;
return res;
}
int main() {
int tcas;
cin >> tcas;
for (int cas = 1; cas <= tcas; ++cas) {
string a, b;
cin >> a >> b;
a = "(" + a + ")";
b = "(" + b + ")";
bool ok = true;
for (M = 0; M < (1<<10); ++M) {
swap(S, a);
N = 0;
int ta = eval();
swap(S, a);
swap(S, b);
N = 0;
int tb = eval();
swap(S, b);
if (ta != tb) {
ok = false;
break;
}
}
cout << "Case " << cas << ": ";
if (ok) cout << "Equivalent" << endl;
else cout << "Not Equivalent" << endl;
}
}