Sort by

recency

|

184 Discussions

|

  • + 0 comments
    import java.io.*;
    import java.util.*;
    import java.lang.reflect.Method;
    
    class BitMe {
        private BitSet b1;
        private BitSet b2;
        private int nbits;
        
        public BitMe(int nbits){
            this.nbits = nbits;
            this.b1 = new BitSet(nbits);
            this.b2 = new BitSet(nbits);        
        }
        
        public void getOperation(String line){
            String[] operation = line.split(" ");
            String cmd = operation[0].toLowerCase();
            String arg1 = operation[1];
            String arg2 = operation[2];
            BitSet target1 = (arg1.equals("1")) ? b1 : b2;
            
            if (!cmd.equals("flip")&&!cmd.equals("set")){
                BitSet target2 = (arg2.equals("1")) ? b1 : b2;
                doABarrelRoll(cmd,target1,target2);
                }
            else {
                int target2 = Integer.parseInt(arg2);
                doABarrelRoll(cmd,target1,target2);
                }   
        }
        
        private void doABarrelRoll(String cmd,BitSet target1,Object bint){
            try {
                
                if (bint instanceof BitSet) {
                    Method method = BitSet.class.getMethod(cmd, BitSet.class);
                    method.invoke(target1, bint);
                } else {
                    Method method = BitSet.class.getMethod(cmd, int.class);
                    method.invoke(target1, bint);
                }
                countBits();
            }
            catch (Exception e){
                System.err.println("cmd:"+cmd);
                e.printStackTrace();
                }
            
        }
        
        private void countBits(){
            int c1 = b1.cardinality();
            int c2 = b2.cardinality();
            System.out.printf("%d %d%n",c1,c2);
        }
        
    }
    
    public class Solution {
    
    
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            int n = sc.nextInt();
            int m = sc.nextInt();sc.nextLine();
            BitMe beatIt = new BitMe(n);
            for (int i = 0; i < m; i++) beatIt.getOperation(sc.nextLine());
            sc.close();
        }
    }
    
  • + 0 comments

    Java 15:

    try (var scanner = new Scanner(System.in)) {
                
                var n = Integer.valueOf(scanner.nextLine().split(" ")[0]);
                var b1 = new BitSet(n);
                var b2 = new BitSet(n);
                var map = Map.of("1", b1, "2", b2);
                
                while(scanner.hasNext()) {
                    var operationParts = scanner.nextLine().split(" ");
                    var operation = operationParts[0];
                    var leftOperand = operationParts[1];
                    var rightOperand = operationParts[2];
                    
                    switch (operation) {
                        case "AND":
                            map.get(leftOperand).and(map.get(rightOperand));
                            break;
                        case "OR":
                            map.get(leftOperand).or(map.get(rightOperand));
                            break;
                        case "XOR":
                            map.get(leftOperand).xor(map.get(rightOperand));
                            break;
                        case "FLIP":
                            map.get(leftOperand).flip(Integer.valueOf(rightOperand));
                            break;
                        default:
                            map.get(leftOperand).set(Integer.valueOf(rightOperand));
                            break;
                    }
                    
                    System.out.printf("%d %d\n", b1.cardinality(), b2.cardinality());
                }
            }
    
  • + 0 comments

    import java.io.; import java.util.;

    public class Solution {

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int m = in.nextInt();
    
        BitSet b1 = new BitSet(n);
        BitSet b2 = new BitSet(n);
    
        HashMap<Integer, BitSet> bitSetHM = new HashMap<Integer, BitSet>() {{
            put(1, b1);
            put(2, b2);
        }};
    
        for (int i = 0; i < m; i++) {
            String cmd = in.next();
            int p = in.nextInt();
            int q = in.nextInt();
    
            switch(cmd) {
                case "AND":
                    bitSetHM.get(p).and(bitSetHM.get(q));
                    break;
                case "OR":
                    bitSetHM.get(p).or(bitSetHM.get(q));
                    break;
                case "XOR":
                    bitSetHM.get(p).xor(bitSetHM.get(q));
                    break;
                case "FLIP":
                    bitSetHM.get(p).flip(q);
                    break;
                case "SET":
                    bitSetHM.get(p).set(q);
                    break;
            }
            System.out.println(bitSetHM.get(1).cardinality()+" "+ bitSetHM.get(2).cardinality());
            if(in.hasNextLine()) {
                in.nextLine();
            }
        }
    
        in.close();
    }
    

    }

  • + 0 comments
    Scanner scan = new Scanner(System.in);
    		int bitLen = scan.nextInt();
    		int operation_count = scan.nextInt();
    
    		BitSet set1 = new BitSet(bitLen);
    		BitSet set2 = new BitSet(bitLen);
    
    		for (int i = 0; i < operation_count; i++) {
    			scan.nextLine();
    			String opreation = scan.next().trim();
    			int opr1 = scan.nextInt();
    			int opr2 = scan.nextInt();
    
    			switch (opreation) {
    			case "AND":
    				if (opr1 == 1)
    					set1.and(set2);
    				else
    					set2.and(set1);
    				break;
    			case "OR":
    				if (opr1 == 1)
    					set1.or(set2);
    				else
    					set2.or(set1);
    				break;
    			case "XOR":
    				if (opr1 == 1)
    					set1.xor(set2);
    				else
    					set2.xor(set1);
    				break;
    			case "FLIP":
    				if (opr1 == 1)
    					set1.flip(opr2);
    				else
    					set2.flip(opr2);
    				break;
    			case "SET":
    				if (opr1 == 1)
    					set1.set(opr2);
    				else
    					set2.set(opr2);
    				break;
    			}
    			
    			System.out.println(set1.cardinality() + " " + set2.cardinality());
    		}
    
  • + 0 comments
    import java.io.*;
    import java.util.*;
    
    public class Solution {
    
        public static void main(String[] args) {
            /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
            Scanner in = new Scanner(System.in);
            int n = in.nextInt();
            int m = in.nextInt();
            
            BitSet b1 = new BitSet(n);
            BitSet b2 = new BitSet(n);
            
            HashMap<Integer, BitSet> bitSetHM = new HashMap<Integer, BitSet>() {{
                put(1, b1);
                put(2, b2);
            }};
            
            for (int i = 0; i < m; i++) {
                String cmd = in.next();
                int p = in.nextInt();
                int q = in.nextInt();
                
                switch(cmd) {
                    case "AND":
                        bitSetHM.get(p).and(bitSetHM.get(q));
                        break;
                    case "OR":
                        bitSetHM.get(p).or(bitSetHM.get(q));
                        break;
                    case "XOR":
                        bitSetHM.get(p).xor(bitSetHM.get(q));
                        break;
                    case "FLIP":
                        bitSetHM.get(p).flip(q);
                        break;
                    case "SET":
                        bitSetHM.get(p).set(q);
                        break;
                }
                System.out.println(bitSetHM.get(1).cardinality()+" "+ bitSetHM.get(2).cardinality());
                if(in.hasNextLine()) {
                    in.nextLine();
                }
            }
                
            in.close();
        }
    }