forked from iamvickynguyen/Kattis-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitbybit.cpp
63 lines (58 loc) · 1.01 KB
/
bitbybit.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
#include <iostream>
#include <vector>
#include <string>
using namespace std;
string bits;
char and_op(char a, char b) {
if (a == '0' || b == '0')
return '0';
if (a == '1' && b == '1')
return '1';
return '?';
}
char or_op(char a, char b) {
if (a == '1' || b == '1')
return '1';
if (a == '0' && b == '0')
return '0';
if (a == '0')
return b;
if (b == '0')
return a;
return '?';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, a, b;
cin >> n;
string unknown;
unknown.resize(32, '?');
while (n) {
bits = unknown;
string instruction;
for (int i = 0; i < n; i++) {
cin >> instruction >> a;
a = 31 - a;
if (instruction == "SET") {
bits[a] = '1';
}
else if (instruction == "CLEAR") {
bits[a] = '0';
}
else if (instruction == "AND") {
cin >> b;
b = 31 - b;
bits[a] = and_op(bits[a], bits[b]);
}
else {
cin >> b;
b = 31 - b;
bits[a] = or_op(bits[a], bits[b]);
}
}
cout << bits << endl;
cin >> n;
}
return 0;
}