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; }
相关文章推荐
- HDU 2746 ——String painter
- sdut 1309 —— 不老的传说问题
- HDU 4283 —— You Are the One
- ZOJ - 3469 —— Food Delivery
- POJ 1651 —— Multiplication Puzzle
- Light oj 1422 - Halloween Costumes
- POJ 2955 —— Brackets
- 求若干矩形的不重叠面积 —— 模拟 + 优化
- 数组删数,求最大得分
- ZOJ - 3537 —— Cake
- Android中ListView的优化
- BZOJ 4516: [Sdoi2016]生成魔咒
- Poj 1050 To the Max
- POJ 3398 Perfect Service【最小支配集修改 dp】
- android欢迎向导页面
- “张冠”不“李戴”,好运上门来
- 燃尽图
- ZOJ - 1453 —— Surround the Trees (求凸包的周长)
- UVAlive - 3516 —— Exploring Pyramids
- UVA - 11806 —— Cheerleaders