CSU-1216: 异或最大值

CSU-1216: 异或最大值_第1张图片

地址:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1216

思路:利用 01字典树,对于每一个数,由高位依次找与异或为1(即于当前位不同的数)的路径,保存其最大值即可

Code :

#include
#include
using namespace std;

const int MAX_S=3200005;
int n,num,ans;
int Trie[MAX_S][2];
bool Sum[MAX_S];

void Insert(int x);
void Find(int x);
int main()
{
	ios::sync_with_stdio(false);
	while(cin>>n){
		memset(Trie,0,sizeof(Trie));
		memset(Sum,0,sizeof(Sum));
		ans=num=0;
		for(int i=0,x;i>x;
			Insert(x);
			Find(x);
		}
		cout<=0;--i)
	{
		t=(x>>i)&1;
		if(!Trie[u][t])	Trie[u][t]=++num;
		u=Trie[u][t];
		Sum[u]=1;
	}
}

void Find(int x)
{
	int u=0,p,t,s=0;
	for(int i=31;i>=0;--i)
	{
		t=(x>>i)&1;
		p=Trie[u][!t];
		if(p&&Sum[p]){
			s=s|(1<

 

你可能感兴趣的:(01字典树)