HDU - 4825 Xor Sum(01字典树)
2017-09-13 20:59
513 查看
Xor Sum
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others)Total Submission(s): 2885 Accepted Submission(s): 1253
Problem Description
Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包含了N个正整数,随后 Prometheus 将向 Zeus 发起M次询问,每次询问中包含一个正整数 S ,之后 Zeus 需要在集合当中找出一个正整数 K ,使得 K 与 S 的异或结果最大。Prometheus 为了让 Zeus 看到人类的伟大,随即同意 Zeus 可以向人类求助。你能证明人类的智慧么?
Input
输入包含若干组测试数据,每组测试数据包含若干行。
输入的第一行是一个整数T(T < 10),表示共有T组数据。
每组数据的第一行输入两个正整数N,M(<1=N,M<=100000),接下来一行,包含N个正整数,代表 Zeus 的获得的集合,之后M行,每行一个正整数S,代表 Prometheus 询问的正整数。所有正整数均不超过2^32。
Output
对于每组数据,首先需要输出单独一行”Case #?:”,其中问号处应填入当前的数据组数,组数从1开始计算。
对于每个询问,输出一个正整数K,使得K与S异或值最大。
Sample Input
2
3 2
3 4 5
1
5
4 1
4 6 5 6
3
Sample Output
Case #1:
4
3
Case #2:
4
Source
2014年百度之星程序设计大赛
- 资格赛
Recommend
liuyiding
#include <bits/stdc++.h> using namespace std; int t, n, m, p = 0; struct xx{ int cnt; xx *nex[2]; xx(){ cnt = 0; memset(nex, NULL, sizeof nex); } }; void Insert(xx *root, long long x){ int s[35]; memset(s, 0, sizeof s); int l = 0; while(x){ s[l++] = x%2; x >>= 1; } reverse(s, s+32); xx *p = root; for(int i = 0; i < 32; i++){ if(p->nex[s[i]] == NULL){ p->nex[s[i]] = new xx(); } p = p->nex[s[i]]; } (p->cnt++); } long long Query(xx *root, long long x){ int s[35]; memset(s, 0, sizeof s); int l = 0, ans = 0; while(x){ s[l++] = x%2; x >>= 1; } reverse(s, s+32); xx *p = root; int i; for(i = 0; i < 32; i++){ if(p->nex[!s[i]]){ p = p->nex[!s[i]]; ans += (!s[i])*(1<<(31-i)); } else if(p->nex[s[i]]){ p = p->nex[s[i]]; ans += (s[i])*(1<<(31-i)); } } return ans; } void Delete(xx *root){ xx *p = root; if(p->nex[0] != NULL) Delete(p->nex[0]); if(p->nex[1] != NULL) Delete(p->nex[1]); delete p; } int main(){ scanf("%d", &t); while(t--){ xx *root = new xx(); scanf("%d%d", &n, &m); for(int i = 0; i < n; i++){ long long x; scanf("%lld", &x); Insert(root, x); } printf("Case #%d:\n", ++p); for(int i = 0; i < m; i++){ long long x; scanf("%lld", &x); printf("%lld\n", Query(root, x)); } Delete(root); } }
相关文章推荐
- HDU 4825 Xor Sum (01字典树模板题)
- HDU 4825 Xor Sum(01字典树入门题)
- hdu Xor Sum 4825 01字典树
- hdu 4825 Xor Sum(01字典树)
- HDU 4825 Xor Sum(01字典树)
- HDU 4825 Xor Sum(01字典树)
- HDU 4825 Xor Sum (01字典树模板题)
- HDU 4825:Xor Sum(01字典树+贪心)
- Xor Sum HDU - 4825 01字典树入门题
- HDU 4825 Xor Sum (01字典树)
- [HDU 4825] Xor Sum(01字典树+贪心)
- [HDU 4825] Xor Sum(01字典树+贪心)
- HDU 4825 Xor Sum (01字典树)
- hdu 4825 Xor Sum(01字典树求最大异或值)
- hdu 4825 Xor Sum (字典树)
- HDU_4825_Xor Sum(字典树)
- hdu 4825 Xor Sum 位操作 字典树
- hdu 4825 Xor Sum (字典树)
- hdu 4825 Xor Sum(字典树)
- HDU-4825 Xor Sum 【字典树+位异或】