CSU 1216 XOR Max

Source: Internet
Author: User

Test instructions

Given sequence Q[1..N], the maximum value of any two-digit XOR

Data range: 1<=n<=10^5,q[i] is a 32-bit non-negative integer

The "Analysis" trie is used to save 0 and 1 from high to low, and then search: Make as much as 1, or 0


WOC Why are multiple sets of data?

#include <cstdio> #include <cstring> #include <cstdlib>using namespace std;const int K=32;const int l= 3300000;typedef long Long ll;int n,ret[k+10]; ll x,mi[k+10];int Nxt[l][2],tot;inline void Do_ret (ll i) {for (int j=k;j+1;j--) i>=mi[j]?ret[j]=1,i-=mi[j]:ret[j]=0;} inline void ins (void) {int now=1;for (int i=k;i+1;i--) {if (!nxt[now][ret[i])) nxt[now][ret[i]]=++tot;now=nxt[now][ret[ I]];}} inline ll Max (ll I,ll j) {return i>j?i:j;} ll DFS (int nn,int nx,int dep) {LL res=0;if (nxt[nn][0]&&nxt[nx][1]) Res=max (Res,dfs (nxt[nn][0],nxt[nx][1), dep-1) +MI[DEP]), if (Nxt[nn][1]&&nxt[nx][0]) Res=max (Res,dfs (nxt[nn][1],nxt[nx][0],dep-1) +MI[DEP]); if (res ) return Res;if (Nxt[nn][0]&&nxt[nx][0]) Res=max (Res,dfs (nxt[nn][0],nxt[nx][0],dep-1)); if (nxt[nn][1]& &AMP;NXT[NX][1]) Res=max (Res,dfs (nxt[nn][1],nxt[nx][1],dep-1)); return res;} int main (void) {mi[0]=1;for (int i=1;i<=k;i++) mi[i]=mi[i-1]*2;for (; ~SCANF ("%d", &n);) {memset (nxt,0,sizeof NXT ); tot=1;for (int i=1;i<=n;i++) {scanf ("%lld", &x);d O_ret (x), ins ();} printf ("%lld\n", DFS (1,1,k));} return 0;}


(1) for the problem of bit operation, the data structure of common maintenance can be trie and segment tree by analyzing the bits.

(2) The nature of the XOR: [1] i^j=k-J^k=i and I^k=j [2] i^i=0,i^0=i-You can convert the prefix and suffix to each other, common applications in the query of durable data structures

CSU 1216 XOR Max

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.