将数字看为01串插入字典树 , 贪心每次尝试走01串的相反的路
#include<iostream> #include <algorithm> #include <cstring> using namespace std; const int N=1e5+4; int ch[N*32][2],tot; int ans; void insert(int x){ int i,c,u=1; for(i=31;i>=0;i--){ c=(x>>i)&1; if(ch[u][c]==0){ ch[u][c]=++tot; } u=ch[u][c]; } } void find(int x){ int i,c,u=1; int t=0; for(i=31;i>=0;i--){ c= (x>>i)&1; c=c^1; if(ch[u][c]) u=ch[u][c],t=t*2+1; else u=ch[u][c^1],t=t*2; } ans=max(ans,t); } main(){ int n,i,x; cin>>n; tot=1; for(i=1;i<=n;i++){ cin>>x; find(x); insert(x); } cout<<ans; }
标签:ch,XOR,int,Largest,tot,1472,ans,Pair,include From: https://www.cnblogs.com/towboa/p/16943089.html