Chip Factory---hdu5536 (xor value max, 01 dictionary tree)

Source: Internet
Author: User

Title Link:

Test instructions: There is an array of a[], containing n number, found from n number of three number to make (A[i]+a[j]) ⊕a[k] The largest, i,j,k different;

The result is the largest, so we can use 01 dictionary tree, first put all the numbers into the dictionary tree, from the n number of two a[i] and A[j],

First remove them from the dictionary tree, and then find and a[i]+a[j] different or maximum number, and the result to take the maximum value;

Finally, do not forget to add a[i] and a[j] to the dictionary tree;

#include <stdio.h>#include<string.h>#include<algorithm>#include<iostream>#include<vector>#include<queue>#include<Set>using namespacestd;#defineMet (A, b) memset (A, B, sizeof (a))#defineN 1005#defineINF 0x3f3f3f3ftypedefLong LongLL;structnode{intnum, sum;///num is the current number, and sum indicates whether it exists;node* next[2];};voidAdd (node* Head,intXintCNT) {Node*p = head;///move the pointer;     for(intI= to; i>=0; i--)    {        intK = (x>>i) &1;///indicates that x corresponds to the binary number from the right number I-bit is K (0 or 1);        if(P->next[k] = = NULL)///Create a new node if it does not exist;{node* Q =Newnode (); P-&GT;NEXT[K] =Q; } P= p->next[k];///move the pointer toward the next node;P->sum + = cnt;///The current node can be reached; update, if the addition of X is +1, delete x 1;} P->num = x;///at the end of the record, the number represented by the head to the current position is x;}intFind (node* Head,intx) {Node*p =Head;  for(intI= to; i>=0; i--)    {        intK = (x>>i) &1; if(p->next[k^1]! = NULL && p->next[k^1]->sum >0)///every time we try to move in different directions, the premise is that there are different nodes;p = p->next[k^1]; ElseP= p->Next[k]; }    return(P-&GT;NUM^X);///the last found P->num is the number that is different from X or Max;}intMain () {intT; scanf ("%d", &T);  while(t--)    {        intN, A[n]; Node*head =Newnode (); scanf ("%d", &N);  for(intI=1; i<=n; i++) {scanf ("%d", &A[i]); Add (Head, A[i],1);///Add all the numbers to the dictionary tree;        }        intAns =0; ///select two numbers a[i] and a[j] from the n number to remove them from the dictionary tree First,///then find and A[i]+a[j] The number of different or maximum, and the maximum value can be obtained;///Finally, do not forget to add a[i] and a[j] to the dictionary tree;         for(intI=1; i<=n; i++) {Add (head, A[i],-1);  for(intj=1; j<=n; J + +)            {                if(I==J)Continue; Add (Head, A[j],-1); intret = Find (head, a[i]+A[j]); Ans=Max (ans, ret); Add (Head, A[j],1); } Add (Head, A[i],1); } printf ("%d\n", ans); }    return 0;}
View Code

Chip Factory---hdu5536 (xor value max, 01 dictionary tree)

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: 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.