题目:
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; }
本文介绍了一个解决 CSU OJ 1216 问题的程序设计方法,通过构建二进制前缀树来实现两个整数的异或最大值查询。使用了 C++ 语言实现,并详细展示了数据结构的设计和核心算法的实现过程。
1782

被折叠的 条评论
为什么被折叠?



