首页 > 其他 > 详细

CSU 1216 —— 异或最大值

时间:2016-03-29 19:15:08      阅读:229      评论:0      收藏:0      [点我收藏+]

题目: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 v = 0, now = 0;
    bool k;
    for (int i = 31; i >= 0; i--)
    {
        k = (x>>i)&1;
        if (a[now].next[!k] != -1)
            k = !k;
        v = v|(k<<i);
        now = a[now].next[k];
    }
    return v;
}

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;
}

 

CSU 1216 —— 异或最大值

原文:http://www.cnblogs.com/AcIsFun/p/5334016.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!