2014 Baidu star qualifying round-XOR SUM

Source: Internet
Author: User

2014 Baidu star qualifying round-XOR SUM

Problem DescriptionZeus and Prometheus played a game. Prometheus gave Zeus a set that contains N positive integers. Then Prometheus will initiate M inquiries to Zeus, each query contains a positive integer S, and Zeus needs to find a positive integer K in the set, so that K and S have the greatest difference or result. In order to let Zeus see the greatness of mankind, Prometheus immediately agreed that Zeus could seek help from humanity. Can you prove human wisdom?

Input contains several groups of test data, each of which contains several rows.
The first line of the input is an integer T (T <10), indicating a total of T groups of data.
The first row of each group of data enters two positive integers N, M (<1 = N, M <= 100000), and the next row contains N positive integers, representing the set obtained by Zeus, in the next M row, each row has a positive integer (S), representing the positive integer queried by Prometheus. All positive integers cannot exceed 2 ^ 32.
Output: for each group of data, you must first Output a separate row. "Case #? : ", Where the question mark should be filled with the current number of data groups. The number of groups starts from 1.
For each query, a positive integer K is output to make K and S exclusive or maximum.
Sample Input

23 23 4 5154 14 6 5 63

Sample Output
Case #1:43Case #2:4

Source 2014 Baidu STAR Program Design Competition-qualifying
AC code: the brute-force solution times out directly. Therefore, select the dictionary tree and select the XOR value for each bit until the optimal solution is obtained;
#include
 
  #include
  
   #include
   
    #define LL long longusing namespace std;LL power[32];typedef struct TrieNode{struct TrieNode *next[2];}TrieNode;void Init(TrieNode **root){*root=NULL;}TrieNode *CreateNode(){TrieNode *p;p=(TrieNode *)malloc(sizeof(TrieNode));if(!p){printf("No enough memory!\n");exit(-1);}p->next[0]=NULL;p->next[1]=NULL;return p;}void InsertNode(TrieNode **root,LL data){int i,k;TrieNode *p=*root;if(p==NULL){p=*root=CreateNode();}for(i=31;i>=0;i--){if(data&power[i])k=1;elsek=0;if(p->next[k]==NULL)p->next[k]=CreateNode();p=p->next[k];}}LL Search(TrieNode *root,LL data){LL ans=0;TrieNode *p=root;for(int i=31;i>=0;i--){if(data&power[i])//the No.i bit is 1{if(p->next[0])//to get the max xor value the same bit should p=p->next[0];             //          be 0 if it existselse// if not exist ,then have to choose 1{ans|=power[i];p=p->next[1];}}else//the No.i bit is 0,so we should choose 1 if it exits{if(p->next[1]){ans|=power[i];p=p->next[1];}elsep=p->next[0];}}return ans;}int main(int argc,char *argv[]){LL t,n,m;scanf("%I64d",&t);for(LL i=1;i<=t;i++){TrieNode *root;power[0]=1;for(int j=1;j<=31;j++)power[j]=power[j-1]<<1;Init(&root);printf("Case #%I64d:\n",i);scanf("%I64d%I64d",&n,&m);while(n--){LL data;scanf("%I64d",&data);InsertNode(&root,data);}while(m--){LL s;scanf("%I64d",&s);printf("%I64d\n",Search(root,s));}}return 0;}
   
  
 


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.