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
Code
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]& &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;}
Summary
(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