题目: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; }
原文:http://www.cnblogs.com/AcIsFun/p/5334016.html