hdu4825 Xor Sum 字典树与异或(经典)
来源:互联网 发布:窗帘销售软件 编辑:程序博客网 时间:2024/06/05 19:38
求某个数与一些数异或的最大值是字典树应用的一个经典问题。
主要思想是贪心,把数字都转化成二进制,把这些数按存到字典树中。从高位开始遍历,如果有不同的边(可以使得异或值为1)肯定走不同的边,如果没有则走与自己值相同的边(一定存在)。边走边统计,最后输出。
这道题题意是:输入一个数组,再给一些数字进行查询,问这些数字与数组中的哪个元素异或值最大。
思路:把输入的数组元素全部插入字典树,然后按照上面说的方法进行查询。
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;const int N = 3000000+5;typedef long long LL;struct Trie { int next[2]; LL v; void init() { v = -1; memset(next, -1, sizeof(next)); };};Trie trie[N];int sz;void init() { sz = 1; trie[0].init();}void myinsert(LL x) { int p = 0; for(int i = 32; i>= 0; i--) { int ch; if(x&(1ll<<i)) ch = 1; else ch = 0; if(trie[p].next[ch] == -1) { trie[p].next[ch] = sz; trie[sz++].init(); } p = trie[p].next[ch]; } trie[p].v = x;}LL query(LL q) { int p = 0; LL ret = 0; for(int i = 32; i >= 0; i--) { int ch = q&(1ll<<i)?1:0; if(trie[p].next[!ch] != -1) { ret |= (1ll<<i); p = trie[p].next[!ch]; } else p = trie[p].next[ch]; } return trie[p].v;}int main() { int t; scanf("%d", &t); for(int cas = 1; cas <= t; cas++) { init(); int n, m; scanf("%d%d", &n, &m); for(int i = 0; i < n; i++) { LL x; scanf("%lld", &x); myinsert(x); } printf("Case #%d:\n", cas); while(m-- > 0) { LL q; scanf("%lld", &q); printf("%lld\n", query(q)); } } return 0;}
0 0
- hdu4825 Xor Sum 字典树与异或(经典)
- hdu4825-01字典树&&贪心&&经典&&异或最大-Xor Sum
- HDU4825 - Xor Sum(Trie 字典树)
- HDU4825 Xor Sum 字典树
- hdu4825 Xor Sum 【字典树】
- HDU4825 Xor Sum 0-1字典树 最大异或和
- Trie树 hdu4825 Xor Sum
- HDU 4825 Xor Sum(01字典树+异或)
- HDU4825 Xor Sum(Trie树 + 贪心)
- HDU4825 Xor Sum(Trie树,二进制)
- hdu4825 Xor Sum【Trie、Xor】
- HDU 4825 Xor Sum(经典01字典树)
- HDU-4825 Xor Sum 【字典树+位异或】
- hdu4825---Xor Sum(Trie + 贪心)
- poj3764 The xor-longest Path 字典树与异或
- HDU-Xor Sum(01字典树)
- HDU 4825 Xor Sum(字典树 经典应用,求一个数与其他数xor最大)
- 2014百度之星第三题Xor Sum(字典树+异或运算)
- hdu1907Jhon(妮姆博奕)
- Adobe经常报错剪贴板错误 copy to clipboard error
- MyBatis(3)Mapper XML文件
- 【Android成长之路】Toast的简单应用
- Objective-C中不同方式实现锁
- hdu4825 Xor Sum 字典树与异或(经典)
- XXX.dll 不是有效的 Office 加载项,解决方法
- 块损坏模拟+恢复
- 手机分配短讯id的面试题目
- Spring注解详解
- Andrew Ng Machine Learning 专题【Recommender Systems】
- Ubuntu12 配置Bugzilla
- 如何在工作的头三年让自己变得强大
- 关于css3动画与H5动画的比较(2015年10月22日)