1】学习了字典树之后,觉得它很明显的就是用空间来换时间,空间复杂度特别大,比如字典数单单存26个小写字母,那么每个节点的孩子节点都有26个孩子节点,字典树中的每一层都保留着不同单词的相同字母。

2】01字典树主要用于解决求异或最值的问题

#include<cstdio>
#include<string>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<cstring>
#include<set>
#include<queue>
#include<algorithm>
#include<vector>
#include<map>
#include<cctype>
#include<stack>
#include<sstream>
#include<list>
#include<assert.h>
#include<bitset>
#include<numeric>
#define debug() puts("++++")
#define gcd(a,b) __gcd(a,b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a,b,sizeof(a))
#define sz size()
#define be begin()
#define mp make_pair
#define pu push_up
#define pd push_down
#define cl clear()
#define lowbit(x) -x&x
#define all 1,n,1
#define rep(i,x,n) for(int i=(x); i<=(n); i++)
#define in freopen("in.in","r",stdin)
#define out freopen("out.out","w",stdout)
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> P;
const ULL base = ;//
const int INF = 0x3f3f3f3f;
const LL LNF = 1e18;
const int maxn = 1e5+;
const int maxm = 1e5 + ;
const double PI = acos(-1.0);
const double eps = 1e-;
const int dx[] = {-,,,,,,-,-};
const int dy[] = {,,,-,,-,,-};
int dir[][] = {{,},{,-},{-,},{,}};
const int mon[] = {, , , , , , , , , , , , };
const int monn[] = {, , , , , , , , , , , , }; int ch[*maxn][]; //边的值
LL val[*maxn]; //点的值
int id; //树中当前结点个数
void init()
{
ms(ch[],);
id=;
}
void Insert(LL x) //在字典树中插入x
{
int u=;
for(int i=;i>=;i--)
{
//和一般的字典树的操作相同将x的二进制插入字典树中
int c=((x>>i)&);
if(!ch[u][c]) //如果结点未被访问过
{
ms(ch[id],); //将当前结点的边值初始化
val[id]=; //结点值为0,表示到此不是一个数
ch[u][c]=id++; //边指向的节点编号
}
u=ch[u][c]; //下一结点
}
val[u]=x; //最后的结点插入val,结点值为x,即到此是一个数
}
LL Query(LL x) //在字典树中查找和x异或的最大的元素并返回值
{
int u=;
for(int i=;i>=;i--)
{
int c=(x>>i)&;
if(ch[u][c^]) //利用贪心,优先寻找和当前位不同的数
u=ch[u][c^];
else
u=ch[u][c];
}
return val[u]; //返回结果
}
int main()
{
int n,m,t,ca=;
LL x;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
init();
for(int i=;i<n;i++)
{
scanf("%lld",&x);
Insert(x);
}
printf("Case #%d:\n",++ca);
while(m--)
{
scanf("%lld",&x);
printf("%lld\n",Query(x));
}
}
return ;
}
/*
【题意】在一组数中找跟某个数异或结果最大的数。 【类型】01字典树 【分析】直接套用模板,将数组中的数插入到 01字典树,对每一个数查询即可。 【时间复杂度&&优化】 【trick】 【数据】
*/

HDU 4185

1. 01字典树是一棵最多 32层的二叉树,其每个节点的两条边分别表示二进制的某一位的值为 0 还是为 1. 将某个路径上边的值连起来就得到一个二进制串。

2.节点个数为 1 的层(最高层)节点的边对应着二进制串的最高位。

3.以上代码中,ch[i] 表示一个节点,ch[i][0] 和 ch[i][1] 表示节点的两条边指向的节点,val[i] 表示节点的值。

4.每个节点主要有 4个属性:节点值、节点编号、两条边指向的下一节点的编号。

5.节点值 val为 0时表示到当前节点为止不能形成一个数,否则 val[i]=数值。

6.节点编号在程序运行时生成,无规律。

7.可通过贪心的策略来寻找与 x异或结果最大的数,即优先找和 x二进制的未处理的最高位值不同的边对应的点,这样保证结果最大。

例题:

一、HDU 4825

传送门:HDU 4825

题目大意:在一组数中找跟某个数异或结果最大的数。

题解:直接套用模板,将数组中的数插入到 01字典树,对每一个数查询即可。

二、HDU 5536

传送门:HDU 5536

题目大意:在一个数组中找出 (s[i]+s[j])^s[k] 最大的值,其中 i、j、k 各不相同。

题解:HDU 5536 题解

三、BZOJ 4260

传送门:BZOJ 4260

题目大意:给你 n 个数,让你求两个不相交的区间元素异或后的和的最大值。

题解:BZOJ 4260 题解

四、POJ 3764

传送门:POJ 3764

题目大意:在树上找一段路径(连续)使得边权相异或的结果最大。

题解:POJ 3764 题解

最新文章

  1. 简单的jQuery幻灯片实现
  2. 学习笔记:HTML5 Canvas绘制简单图形
  3. JAVA模拟HTTP post请求上传文件
  4. BZOJ3740 : pku2842 N-dimension Matching N维匹配
  5. ssh问题
  6. HDU 4898 The Revenge of the Princess’ Knight ( 2014 Multi-University Training Contest 4 )
  7. Tableview的更新和删除某一行
  8. CSS布局属性
  9. js 无刷新分页代码
  10. angular ng-bind-html 对src路径失效 解决方案
  11. 树莓派wiringPi,BCM,BOARD编码对应管脚
  12. C++ template一些体悟(1)
  13. java程序的三种结构
  14. 【题解】Luogu P1972 [SDOI2009]HH的项链
  15. 高可用集群(crmsh详解)http://www.it165.net/admin/html/201404/2869.html
  16. C# 常用字符串处理办法
  17. Java 代理模式(一) 静态代理
  18. 二十二、详述 IntelliJ IDEA 中恢复代码的方法
  19. IO 流之字节流和转换流
  20. CentOS环境下yum安装LAMP

热门文章

  1. 温习js中对象的继承
  2. UVA 1645 Count
  3. 数学:GCD
  4. LightOJ 1085 - All Possible Increasing Subsequences 树状数组+离散
  5. elk相关
  6. [LA3523/uva10195]圆桌骑士 tarjan点双连通分量+奇环定理+二分图判定
  7. HDU 1548 A strange lift (广搜)
  8. java 连接数据库报错:Caused by: com.mysql.cj.exceptions.InvalidConnectionAttributeException: The server time zone value &#39;
  9. 往Layout中动态添加View
  10. 分类算法:决策树(C4.5)(转)