# CSU-1216: 异或最大值-trie-01字典树

## Output

RunID User Problem Result Memory Time Language Code Length Submit Time
200255 liuyuhong 1216 Accepted 3648 140 C++/Edit 1930 B 2016-08-10 02:38:44

``````#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <iostream>
#include <queue>
#include <map>
#include <vector>
using namespace std;
#define inf 2147483647
const int maxnode = 100005*32;

int len;
struct trie
{
int ch[maxnode][2];
// int val[maxnode];
int sz;
void init()
{
sz=1;
//    val[0]=0;		//可不需要
memset(ch[0],0,sizeof(ch[0]));
}
void insert(int s )
{
int u=0,n=31,i;
for (i=0; i<n; i++)
{
int c=s&(1<<(30-i));
c=!!c;
if (!ch[u][c])
{
memset(ch[sz],0,sizeof(ch[sz]));
// val[sz]=0;
ch[u][c]=sz++;
}
u=ch[u][c];
}
// val[u]=num;
}

int query_if(int s)	//查找当前单词是否存在,本题用不上
{
int ret=0;
int u=0,n=31,i;
int nex;
for (i=0; i<n; i++)
{
int c=s&(1<<(30-i));c=!!c;
if (ch[u][c^1])
c^=1;
ret|=c<<(30-i);
u=ch[u][c];
}
return ret;
}

};
trie tp;
int x[100005];
int main()
{
int n,i,j;
while(scanf("%d",&n)!=EOF)
{

tp.init();
for (i=1; i<=n; i++)
{
scanf("%d",&x[i]);
tp.insert(x[i]);
}
int ans=0;
for (i=1; i<=n; i++)
{
ans=max(ans,x[i]^tp.query_if(x[i]));
}
printf("%d\n" ,ans);//输出结果忘记取模wa几次

}

return 0;
}
``````

原文作者：Trie树
原文地址: https://blog.csdn.net/viphong/article/details/52167649
本文转自网络文章，转载此文章仅为分享知识，如有侵权，请联系博主进行删除。