-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjava-bitset.java
55 lines (49 loc) · 1.79 KB
/
java-bitset.java
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
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int bits = scanner.nextInt();
int operations = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
BitSet bitSet1 = new BitSet(bits);
BitSet bitSet2 = new BitSet(bits);
for (int i = 0; i < operations; i++) {
String[] rowInput = scanner.nextLine().split(" ");
if (rowInput[0].equals("AND")) {
if (rowInput[1].equals("1")) {
bitSet1.and(bitSet2);
} else {
bitSet2.and(bitSet1);
}
} else if (rowInput[0].equals("SET")) {
int bit = Integer.parseInt(rowInput[2]);
if (rowInput[1].equals("1")) {
bitSet1.set(bit);
} else {
bitSet2.set(bit);
}
} else if (rowInput[0].equals("FLIP")) {
int bit = Integer.parseInt(rowInput[2]);
if (rowInput[1].equals("1")) {
bitSet1.flip(bit);
} else {
bitSet2.flip(bit);
}
} else if (rowInput[0].equals("OR")) {
if (rowInput[1].equals("1")) {
bitSet1.or(bitSet2);
} else {
bitSet2.or(bitSet1);
}
} else if (rowInput[0].equals("XOR")) {
if (rowInput[1].equals("1")) {
bitSet1.xor(bitSet2);
} else {
bitSet2.xor(bitSet1);
}
}
System.out.println(bitSet1.cardinality() + " " + bitSet2.cardinality());
}
}
}