- Prepare
- Java
- Data Structures
- Java BitSet
- Discussions
Java BitSet
Java BitSet
+ 17 comments Using lambdas:
public static void main(String[] args) { Scanner sc = new Scanner(System.in); int setSize = sc.nextInt(); int noOps = sc.nextInt(); BitSet[] bitSets = new BitSet[]{new BitSet(setSize), new BitSet(setSize)}; Map<String, BiConsumer<Integer,Integer>> ops = new HashMap<>(); ops.put("AND", (index1, index2) -> bitSets[index1-1].and(bitSets[index2-1])); ops.put("OR", (index1, index2) -> bitSets[index1-1].or(bitSets[index2-1])); ops.put("XOR", (index1, index2) -> bitSets[index1-1].xor(bitSets[index2-1])); ops.put("SET", (index1, index2) -> bitSets[index1-1].set(index2)); ops.put("FLIP", (index1, index2) -> bitSets[index1-1].flip(index2)); for (int i = 0; i < noOps; i++){ ops.get(sc.next()).accept(sc.nextInt(), sc.nextInt()); System.out.println(bitSets[0].cardinality() + " " + bitSets[1].cardinality()); } }
+ 6 comments Lots of clever solutions here. Here's my refactored version after stealing some gems from these discussions. Thanks guys! ;-)
import java.util.BitSet; import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner get = new Scanner(System.in); int n = get.nextInt(); int m = get.nextInt(); BitSet b1 = new BitSet(n); BitSet b2 = new BitSet(n); BitSet[] bitset = new BitSet[3]; bitset[1] = b1; bitset[2] = b2; while ( 0 < m-- ) { String op = get.next(); int x = get.nextInt(); int y = get.nextInt(); switch (op) { case "AND": bitset[x].and(bitset[y]); break; case "OR": bitset[x].or(bitset[y]); break; case "XOR": bitset[x].xor(bitset[y]); break; case "FLIP": bitset[x].flip(y); break; case "SET": bitset[x].set(y); } System.out.printf("%d %d%n", b1.cardinality(), b2.cardinality()); } } }
+ 3 comments I think there's a tricky "bug" here which can catch out the unwary, particularly those that didn't know about BitSet.cardinality().
The bit addresses for SET and FLIP : are they 0 based or 1 based ?
The 7th operation on TestCase 2 is SET 1 0, which implies it's 0 based. The 4840th operation on TestCase 6 - with a BitSet size of 523 - is FLIP 2 523, which implies it's 1 based.
The BitSet automatically grows to a nice size for storage : so new BitSet(523) actually gives you a BitSet of 576 bits long - which means it works with either 0 based or 1 based, and you can set or flip bits at higher addresses than you should really be able to.
I initially didn't know about BitSet.cardinality(), so was manually counting the bits in the original range : which meant I looped over 0 -> 522 and so didn't picked up the flipped bit in test set 6.
Can I suggest the datasets be reviewed, so they are consistently 0 based ? In which case Test Case 6 (at least) is invalid ?
+ 1 comment I've had this problem on several challenges now: for some testcases my scanner doesn't seem to want to read the last line of the given input. I recognized this problem when trying out testcase 5, which was causing some unknown runtime error, and realized the last line of the input wasn't recognized until I pressed the "enter" key, after which it computed the result normally. Any idea what might cause this problem and how to solve it?
+ 5 comments i am getting "error : illegal character :\127" please tell me what is wrong with my code
public static void main(String[] args) {
Scanner sc = new Scanner(System.in); int size = sc.nextInt(); BitSet b1 = new BitSet(size); BitSet b2 = new BitSet(size); int op = sc.nextInt(); String s = null; int i,j; while(op>0){ s = sc.next(); i = sc.nextInt(); j = sc.nextInt(); switch(s){ case("AND") : if(i==1) { b1.and(b2);} else b2.and(b1); break; case("OR") : if(i==1) { b1.or(b2);} else b2.or(b1); break; case("XOR") : if(i==1) { b1.xor(b2);} else b2.xor(b1); break; case("FLIP") : if(i==1) { b1.flip(j);} else b2.flip(j); break; case("SET") : if(i==1) { b1.set(j);} else b2.set(j); break; default : ; } System.out.println(b1.cardinality()+" "+b2.cardinality()); op--; } }
Sort 119 Discussions, By:
Please Login in order to post a comment