CSU 1216 —— 异或最大值

题目: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;
}

 

posted on 2016-03-29 18:34  SuperChan  阅读(292)  评论(0编辑  收藏  举报

导航