#异或对 #Trie 143. 最大异或对

在给定的N个整数A1,A2……AN中选出两个进行xor(异或)运算,得到的结果最大是多少?

.

输入格式
第一行输入一个整数N。

第二行输入N个整数A1~AN。

输出格式
输出一个整数表示答案。

数据范围
1≤N≤105,
0≤Ai<231
输入样例:
3
1 2 3
输出样例:
3
#include<bits/stdc++.h>
using namespace std;
const int N=100090;
int  pos=1,a[N],T[3000010][2];
void Insert(int x){
	int p=0;
	for(int j=30;~j;j--){
		int n=x>>j&1;
		if(!T[p][n]) T[p][n]=pos++;
		p=T[p][n];	
	}
}
int Find(int x){
	int res=0,p=0;
	for(int j=30;~j;j--){
		int n=x>>j & 1;
		if(T[p][!n]){
			res+=1<<j;
			p=T[p][!n];
		}
		else p=T[p][n];
	}
	return res;
}
int main(){
	int n,res=0;
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		Insert(a[i]); 
	}
	for(int i=1;i<=n;i++){
		res=max(res,Find(a[i]));
	}
	cout<<res<<endl;
	return 0;
}
原文地址:https://www.cnblogs.com/yuanyulin/p/14026781.html