您的位置:首页 > 其它

CSU 1216 —— 异或最大值

2016-05-18 21:36 435 查看
题目:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1216

#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;

struct Node {
int next[2];
} a[31*100000+5];

int tot;

void add(int x)
{
int t, cur=0;
for(int i=31; i>=0; i--) {
t = (x>>i)&1;
if(a[cur].next[t] == -1)
a[cur].next[t] = ++tot;
cur = a[cur].next[t];
}
}

int f(int x)
{
int ret = 0, cur = 0, t;
for(int i=31; i>=0; i--) {
t = (x>>i)&1;
if(a[cur].next[!t] != -1) {
t = !t;
}
ret |= t<<i;
cur = a[cur].next[t];
}
return ret;
}

int main ()
{
int n, x, ans;
while(scanf("%d", &n) != EOF) {
tot = 0;
ans = -1;
memset(a, -1, sizeof(a));
for(int i=0; i<n; i++) {
scanf("%d", &x);
add(x);
ans = max(ans, x^f(x));
}
printf("%d\n", ans);
}

return 0;
}


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: