hdu4825---Xor Sum(Trie + 贪心)
来源:互联网 发布:淘宝直播间入口 编辑:程序博客网 时间:2024/05/17 04:50
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 | We have carefully selected several similar problems for you: 5235 5234 5233 5232 5231
把n个数化成二进制,然后建立Trie树
接下来贪心遍历这棵树就行
/************************************************************************* > File Name: hdu4825.cpp > Author: ALex > Mail: zchao1995@gmail.com > Created Time: 2015年05月25日 星期一 14时36分41秒 ************************************************************************/#include <functional>#include <algorithm>#include <iostream>#include <fstream>#include <cstring>#include <cstdio>#include <cmath>#include <cstdlib>#include <queue>#include <stack>#include <map>#include <bitset>#include <set>#include <vector>using namespace std;const double pi = acos(-1.0);const int inf = 0x3f3f3f3f;const double eps = 1e-15;typedef long long LL;typedef pair <int, int> PLL;int bit[40];static const int MaxNode = 3200010;class Trie { public: void init(); int newnode(); void insert(int bit[], int w); int getNum(int bit[], int w); private: int next[MaxNode][2]; int end[MaxNode]; int L, root;}trie;int Trie :: newnode() { next[L][0] = next[L][1] = -1; ++L; return L - 1;}void Trie :: init() { L = 0; root = newnode();}void Trie :: insert(int bit[], int w) { int now = root; for (int i = 31; i >= 0; --i) { if (next[now][bit[i]] == -1) { next[now][bit[i]] = newnode(); } now = next[now][bit[i]]; } end[now] = w;}int Trie :: getNum(int bit[], int w) { int now = root; for (int i = 31; i >= 0; --i) { if (next[now][bit[i] ^ 1] == -1) { now = next[now][bit[i]]; } else { now = next[now][bit[i] ^ 1]; } } return end[now];};void calc(int num) { int cnt = 0; while (num) { bit[cnt++] = num % 2; num >>= 1; } for (int i = cnt; i <= 31; ++i) { bit[i] = 0; } }int main() { int t, icase = 1; scanf("%d", &t); while (t--) { int n, m, w; scanf("%d%d", &n, &m); trie.init(); for (int i = 1; i <= n; ++i) { scanf("%d", &w); calc(w); trie.insert(bit, w); } printf("Case #%d:\n", icase++); for (int i = 1; i <= m; ++i) { scanf("%d", &w); calc(w); printf("%d\n", trie.getNum(bit, w)); } } return 0;}
- hdu4825---Xor Sum(Trie + 贪心)
- HDU4825 Xor Sum(Trie树 + 贪心)
- hdu4825 Xor Sum【Trie、Xor】
- Trie树 hdu4825 Xor Sum
- HDU4825 - Xor Sum(Trie 字典树)
- HDU4825 Xor Sum(Trie树,二进制)
- HDU4825 Xor Sum 字典树
- hdu4825 Xor Sum 【字典树】
- HDU 4825 Xor Sum(trie树+贪心)
- hdu4825-01字典树&&贪心&&经典&&异或最大-Xor Sum
- hdu4825 Xor Sum 字典树与异或(经典)
- HDU4825 01trie+二进制
- [codevs 1187]Xor最大路径 Trie+贪心
- HDU 4825 Xor Sum [trie]【思维&字符串】
- Poj 3764 The xor-longest Path(Trie树+xor+贪心)
- HDU4825 Xor Sum 0-1字典树 最大异或和
- hdu4825 字典树 + 贪心
- hdu4825(Trie树+位运算)
- c++返回函数局部对象的引用
- \r \n \r\n的区别
- java实现 SSL双向认证
- 大家嘴上所说的互联网+
- Appium TestNg Maven Android Eclipse java简单启动实例
- hdu4825---Xor Sum(Trie + 贪心)
- 第十二周 项目2-摩托车继承自行车和机动车
- SNTP 获取时间服务器UTC时间
- 余弦距离、欧氏距离和杰卡德相似性度量的对比分析
- UIWebView与动态创建Cell
- IOS-TextField知多少
- WSAAccept与WSAAccept的ConditionAcceptFunction
- JSON.parse和eval的区别
- UICollectionViewLayout