-1

this question was asked in acm-icpc in 2009. I have solution in java but why i am getting wrong answer.Please help me in find out the error. Is trie construction is fine and to find maximum with trie is right. i am trying it first time

Problem Link

import java.io.*;
import java.util.*;
public class Main{
public static void main(String []args){
    Scanner sc=new Scanner(System.in);
    //System.out.print((4&(1L<<3)));
    int tes=sc.nextInt();
    while(tes-->0){
        int size=sc.nextInt();
    long arr[]=new long[size];
    for(int i=0;i<size;i++){
        arr[i]=sc.nextLong();
    }
    long pre=0;
    long res=0;
    Trie t=new Trie();
    t.insert(0);
    for(int i=0;i<size;i++){
        pre=pre^arr[i];
        //System.out.println(Integer.toBinaryString(pre));
        t.insert(pre);
        long y=t.query(pre);
        y=y^pre;
        if(y>res){
            res=y;
        }
    }
    System.out.print(res);}

    }
 }
 class TrieNode{
 long val;
 TrieNode left;
 TrieNode right;
  TrieNode(){

 }
 TrieNode(int i){
     this.val=i;
 }
 }
 class Trie{
 private final TrieNode root;
 Trie(){
    root=new TrieNode();
    root.val=-1;
 }
  void insert(long num){
     TrieNode t=root;
    for(int i=63;i>=0;i--){
        if(((num>>i)&1)!=0){
            if(t.right==null){
                TrieNode temp=new TrieNode();
                t.right=temp;
            }
            t=t.right;
        }
        else{
            if(t.left==null){
                TrieNode temp=new TrieNode();
                t.left=temp;
            }
            t=t.left;
        }
        t.val=num;
    }

}
long query(long num){
    int i=0;
    TrieNode t=root;
    for(i=63;i>=0;i--){
        if(((num>>i)&1)==0){
            if(t.right!=null){
                t=t.right;
            }
            else t=t.left;
        }
        else{
            if(t.left!=null){
                t=t.left;
            }
            else t=t.right;
        }
    }
    return t.val;
   }
  }
  • You should include a summary of the problem and not just link to an external resource. Furthermore, proper formatting makes it easier for other users to read your code. – beatngu13 Oct 22 '16 at 21:38

1 Answers1

0

Edit output line.

System.out.println(res);

or

System.out.print(res+"\n");
Rafik Farhad
  • 1,182
  • 2
  • 12
  • 21