hdu Xor Sum 4825 01字典树
来源:互联网 发布:剑三怎样导入捏脸数据 编辑:程序博客网 时间:2024/06/05 10:10
思路:把每个数按照其二进制位是1还是0插入到字典树中.在这里用二维数组储存。用一个全局变量cnt表示存放位置,每个数显然占了32个位置。
比如要存放3 4 5三个数,i表示数组第i行。
i=0 1 0
i=1 2 0
i=2 3 0
i=3 4 0
i=4 5 0
i=5 6 0
i=6 7 0
i=7 8 0
i=8 9 0
i=9 10 0
i=10 11 0
i=11 12 0
i=12 13 0
i=13 14 0
i=14 15 0
i=15 16 0
i=16 17 0
i=17 18 0
i=18 19 0
i=19 20 0
i=20 21 0
i=21 22 0
i=22 23 0
i=23 24 0
i=24 25 0
i=25 26 0
i=26 27 0
i=27 28 0
i=28 29 0
i=29 30 33
i=30 0 31
i=31 0 32
i=32 0 0
i=33 34 0
i=34 35 36
每次询问尽量往与自己当前位相反值走。
相当于在字典树中走跳棋一样,v[i]存有最后到头位置的
写法一:
#include <bits/stdc++.h>using namespace std;typedef long long LL;const int MAXN = 3500005;int a[MAXN];int v[MAXN];int G[MAXN][2];int cnt;void Insert(int x, int id){ int u=0; for(int i=30;i>=0;--i) { int flag=(((1<<i)&x)!=0); if(!G[u][flag]) G[u][flag]=cnt++; u=G[u][flag]; } v[u]=id;}int slove(int x){ int u=0; for(int i=30;i>=0;--i) { int flag=(((1<<i)&x)!=0); if(G[u][flag^1]) u=G[u][flag^1]; else u=G[u][flag]; } return a[v[u]];}int main(){ int t; int n, m; scanf("%d", &t); for(int cas=1; cas<=t; ++cas) { memset(G,0,sizeof(G)); memset(v,0,sizeof(v)); cnt=1; scanf("%d %d", &n, &m); for(int i=1;i<=n;++i) { scanf("%d", &a[i]); Insert(a[i], i); } printf("Case #%d:\n", cas); while(m--) { int x; scanf("%d", &x); printf("%d\n", slove(x)); } } return 0;}
写法二:字典树
#include <iostream>#include <algorithm>#include <vector>#include <cmath>#include <iomanip>#include <cstring>#include <cstdio>using namespace std;const int MAXN = 50010;struct node{ int x; node *next[2];};void dict_insert(int x, node *T){ node *p=T; for(int i=31;i>=0;--i) { int k=(x>>i)&1; if(p->next[k]==NULL) { node *q=(node*)malloc(sizeof(node)); memset(q->next,0,sizeof(q->next)); p->next[k]=q; } p=p->next[k]; } p->x=x;}int query(int x, node *T){ node *p=T; for(int i=31;i>=0;--i) { int k=(x>>i)&1; if(p->next[k^1]!=NULL) { p=p->next[k^1]; } else p=p->next[k]; } return p->x;}int main(){ int t, n, m, x; scanf("%d", &t); for(int Case =1; Case<=t; ++Case) { scanf("%d %d", &n, &m); node *T=(node*)malloc(sizeof(node)); memset(T->next,0,sizeof(T->next)); for(int i=1; i<=n; ++i) { scanf("%d", &x); dict_insert(x, T); } printf("Case #%d:\n", Case); while(m--) { scanf("%d", &x); printf("%d\n", query(x, T)); } }}
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
- 【01字典树】HDU 4825 Xor Sum
- HDU 4825 Xor Sum 01字典树
- HDU 4825 Xor Sum (01字典树)
- HDU 4825 Xor Sum(01字典树)
- HDU 4825 Xor Sum 【01字典树】
- hdu Xor Sum 4825 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 (字典树)
- HDU-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(经典01字典树)
- Ubuntu 16.04编译安装最新的OpenCV3.2
- GIF屏幕录像工具
- 基于微博数据用 Python 打造一颗“心”
- 基于java的Selenium基础
- [Java并发编程]-二、线程信息获取与设置
- hdu Xor Sum 4825 01字典树
- java递归删除文件或文件夹
- 网络编程
- 腾讯面试官送给准程序员的一些建议!
- 两个互联网公司的创业故事
- Class.forName蹊跷的错误,及修改方式
- Windows 2008 R2 配置 DNS 实现二级域名
- Android JNI 自己遇到的问题记录
- db2并列排序