Sort by

recency

|

187 Discussions

|

  • + 0 comments
    public static void main(String[] args) {
    
            Scanner scnr = new Scanner(System.in);
            Integer N = scnr.nextInt();
            Integer M = scnr.nextInt();
    
            BitSet[] b = new BitSet[] {new BitSet(N), new BitSet(N)};  
    
            for (int i = 0; i < M; i++) {
                String op = scnr.next();
                Integer p1 = scnr.nextInt();
                Integer p2 = scnr.nextInt();
                
                if (op.equals("AND")) {
                    b[p1-1].and(b[p2-1]);
                } else if (op.equals("OR")) {
                    b[p1-1].or(b[p2-1]);
                } else if (op.equals("XOR")) {
                    b[p1-1].xor(b[p2-1]);
                } else if (op.equals("FLIP")) {
                    b[p1-1].flip(p2);
                } else if (op.equals("SET")) {
                    b[p1-1].set(p2);
                }
                System.out.printf("%d %d\n", b[0].cardinality(), b[1].cardinality());
            }
            
            scnr.close();
        }
    
  • + 0 comments

    Best digital marketing agency in varanasi https://sparkinwords.com/web-development/ To develop and grow your business.

  • + 0 comments
    import java.io.*;
    import java.util.*;
    
    public class Solution {
    
        public static void main(String[] args) {
            Scanner s = new Scanner(System.in);
            int n = s.nextInt(), m = s.nextInt();
            BitSet b1 = new BitSet(n), b2 = new BitSet(n);
            
            while (m-- > 0) {
                String op = s.next();
                int i1 = s.nextInt(), i2 = s.nextInt();
                
                switch (op) {
                    case "AND": if (i1 == 1) b1.and(i2 == 1 ? b1 : b2); else b2.and(i2 == 1 ? b1 : b2); break;
                    case "OR":  if (i1 == 1) b1.or(i2 == 1 ? b1 : b2); else b2.or(i2 == 1 ? b1 : b2); break;
                    case "XOR": if (i1 == 1) b1.xor(i2 == 1 ? b1 : b2); else b2.xor(i2 == 1 ? b1 : b2); break;
                    case "FLIP": if (i1 == 1) b1.flip(i2); else b2.flip(i2); break;
                    case "SET": if (i1 == 1) b1.set(i2); else b2.set(i2); break;
                }
                System.out.println(b1.cardinality() + " " + b2.cardinality());
            }
        }
    }
    
  • + 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());
                }
            }