0
public class ACM_ICPC_TEAM {
 public static void main(String[] args) {

        Scanner sc=new Scanner(System.in);
        int N=sc.nextInt();
        int M=sc.nextInt();
        sc.nextLine();

        String s[]=new String[N];
        for(int i=0;i<N;i++)
        { 
        for(int j=0;j<M;j++);
        {               
            s[i]=sc.nextLine().toString();              
        }         
        }
      BitSet b1=new BitSet(M);
      BitSet b2=new BitSet(M);
      BitSet b3=new BitSet(M);
      int max=0,count=0,count1=0;
        for(int i=0;i<N;i++)
        {  b1=fromString(s[i]);
         for(int j=i+1;j<N;j++)
         {  
            b3=(BitSet) b1.clone();
            b2=fromString(s[j]);
            b3.or(b2);       
            count=b3.cardinality();    
            if(count>max)
                {
                max=count;
                count1=1;
                }
            else if(count==max)
                 count1++;
         }
        }

        System.out.println(count+"\n"+count1);          
    }

 public static BitSet fromString(String s)
 {  
    return BitSet.valueOf(new long[] { Long.parseLong(s,2) });
 }
 public static String toString(BitSet b)
 {
 return Long.toString(b.toLongArray()[0], 2);   
 }
}

My fromString is giving me NumberFormatException when I pass binary string of lage size(~500), its working absolutely fine for smaller length strings.

3
  • Just a guess: You read next int, but the number might be bigger. Commented Sep 8, 2016 at 16:26
  • 3
    Please edit your question to include the actual text of the exception, including the stack trace. Indicate which line of the code you posted is triggering the exception. Commented Sep 8, 2016 at 16:53
  • 1
    Please have a look at How to create a Minimal, Complete, and Verifiable example, and review your post. Commented Sep 8, 2016 at 16:54

2 Answers 2

1

A Java long value is a 64 bit number, i.e. you can store up to 64 bits in it. A String with 500 bits simply cannot be represented by a single long, thus the exception.

A BitSet can represent sets of bits of arbitrary length. Unfortunately, BitSet does not have a method to append one BitSet to another. Therefore, what I would do is go through the String char by char, and set or clear the n'th bit of the BitSet, as needed.

Sign up to request clarification or add additional context in comments.

Comments

0

There's no way to create an int with 500 bits. The maximum is 64. In Java you can use BigInteger which is a class which is used for huge numbers.

It has a constructor which takes a String and a constructor which takes a String value and int radix.

BigInteger a = new BigInteger("1234567890123456");
BigInteger b = new BigInteger("10000000000000000", 2);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.