Trie树 & 01Trie
指针版
#define MAXNUM 26
//定义字典树结构体
typedef struct Trie
{
bool flag;//从根到此是否为一个单词
Trie *next[MAXNUM];
}Trie;
//声明一个根
Trie *root;
//初始化该根
void init()
{
root = (Trie *)malloc(sizeof(Trie));
root->flag=false;
for(int i=;i<MAXNUM;i++)
root->next[i]=NULL;
}
//对该字典树的插入单词操作
void insert(char *word)
{
Trie *tem = root;
while(*word!='\0')
{
if(tem->next[*word-'a']==NULL)
{
Trie *cur = (Trie *)malloc(sizeof(Trie));
for(int i=;i<MAXNUM;i++)
cur->next[i]=NULL;
cur->flag=false;
tem->next[*word-'a']=cur;
}
tem = tem->next[*word-'a'];
word++;
}
tem->flag=true;
}
//查询一个单词的操作
bool search(char *word)
{
Trie *tem = root;
for(int i=;word[i]!='\0';i++)
{
if(tem==NULL||tem->next[word[i]-'a']==NULL)
return false;
tem=tem->next[word[i]-'a'];
}
return tem->flag;
}
//释放字典树内存操作,由于本题测试数据后程序自动跳出,所以这里没写释放内存函数
void del(Trie *cur)
{
for(int i=;i<MAXNUM;i++)
{
if(cur->next[i]!=NULL)
del(cur->next[i]);
}
free(cur);
}
01Trie
struct Trie
{
int root, tot, next[*][], cnt[*], end[*]; inline int Newnode()
{
memset(next[tot], -, sizeof(next[tot]));
cnt[tot] = ;
end[tot] = ;
return tot ++;
} inline void Init()
{
tot = ;
root = Newnode();
} inline void Insert(int x)
{
int p = root;
cnt[p] ++;
for(int i = ; i >= ; i --)
{
int idx = (x >> i) & ;
if(next[p][idx] == -)
next[p][idx] = Newnode();
p = next[p][idx];
cnt[p] ++;
}
end[p] = x;
} inline void Del(int x)
{
int p = root;
cnt[p] --;
for(int i = ; i >= ; i --)
{
int idx = (x >> i) & ;
p = next[p][idx];
cnt[p] --;
}
} inline int Search(int x) //求x的异或最大值,看情况修改
{
int p = root;
for(int i = ; i >= ; i --)
{
int k = (x >> i) & ;
if(next[p][k^] != - && cnt[next[p][k^]])
p = next[p][k^];
else
p = next[p][k];
}
return x ^ end[p];
}
}tr;
Trie树 & 01Trie的更多相关文章
- Luogu P2922 [USACO08DEC]秘密消息Secret Message 字典树 Trie树
本来想找\(01Trie\)的结果找到了一堆字典树水题...算了算了当水个提交量好了. 直接插入模式串,维护一个\(Trie\)树的子树\(sum\)大小,求解每一个文本串匹配时走过的链上匹配数和终点 ...
- Trie树入门
Trie树入门 貌似很多人会认为\(Trie\)是字符串类型,但是这是数据结构!!!. 详情见度娘 下面开始进入正题. PS:本文章所有代码未经编译,有错误还请大家指出. 引入 先来看一个问题 给 ...
- 【bzoj3217】ALOEXT 替罪羊树套Trie树
题目描述 taorunz平时最喜欢的东西就是可移动存储器了……只要看到别人的可移动存储器,他总是用尽一切办法把它里面的东西弄到手. 突然有一天,taorunz来到了一个密室,里面放着一排可移动存储器, ...
- Trie树(小)总结 By cellur925
关于\(Trie\)树的详细介绍,还请移步这篇深度好文 基本操作 插入 void insert() { int p=0; int len=strlen(tmp+1); for(int i=1;i< ...
- BZOJ 4260: Codechef REBXOR (trie树维护异或最大值)
题意 分析 将区间异或和转化为前缀异或和.那么[L,R][L,R][L,R]的异或和就等于presum[R] xor presum[L−1]presum[R]\ xor \ presum[L-1]pr ...
- [CSP-S模拟测试]:big(Trie树+贪心)
题目描述 你需要在$[0,2^n)$中选一个整数$x$,接着把$x$依次异或$m$个整数$a_1~a_m$.在你选出$x$后,你的对手需要选择恰好一个时刻(刚选完数时.异或一些数后或是最后),将$x$ ...
- 【题解】HDU5845 Best Division (trie树)
[题解]HDU5845 Best Division (trie树) 题意:给定你一个序列(三个参数来根),然后请你划分子段.在每段子段长度小于等于\(L\)且子段的异或和\(\le x\)的情况下最大 ...
- The XOR Largest Pair (trie树)
题目描述 在给定的 NN 个整数 A_1,A_2,--,A_NA1,A2,--,AN 中选出两个进行xor运算,得到的结果最大是多少?xor表示二进制的异或(^)运算符号. 输入格式 第一行输入 ...
- 基于trie树做一个ac自动机
基于trie树做一个ac自动机 #!/usr/bin/python # -*- coding: utf-8 -*- class Node: def __init__(self): self.value ...
随机推荐
- 一、PBNI环境搭建及初步使用
PowerBuilder Native Interface(PowerBuilder本机接口PBNI)允许将第3方程序转换为PowerBuilder对象,供PowerBuilder直接使用,也允许将P ...
- C++ Const(常类型)的作用总结
C++ Const的作用总结 面试或者工作中,我们经常遇到const修饰符定义的变量,函数和对象等.那么const的作用具体是什么,有哪些.今天自己好好的总结了一下,记录下来方便自己以后时间久了不记得 ...
- [ASP.NET MVC] 使用CLK.AspNet.Identity提供以角色为基础的访问控制(RBAC)
[ASP.NET MVC] 使用CLK.AspNet.Identity提供以角色为基础的访问控制(RBAC) 程序代码下载 程序代码下载:点此下载 前言 ASP.NET Identity是微软所贡献的 ...
- DOM LOAD测试笔记
DOM时间:1823ms LOAD时间:4912ms COMP时间:5427ms 1585 4757 5650 1859 3487 3910 1600 4648 5099 1610 4428 4878 ...
- 使用HyperV虚拟机装系统
新建虚拟机 新建虚拟机 进行相关参数设置 选择系统安装镜像位置,名称及位置 指定代数一般为1代即可 为虚拟机运行分配内存 创建虚拟硬盘或连接已有虚拟硬盘,并分配硬盘空间 核对创建虚拟机相关信息 安装系 ...
- 关于SharePoint 的Client object model该何时load和execut query的一点自己的看法
很多人在用client object model的时候,不知道何时或者该不该load,今天看到一个观点描述这个问题,觉得很有道理,和大家分享.那就是写client object model就像写sql ...
- 桥牌笔记:Skill 4 Series A–Deal 5
南主打5C. 此牌的难点在于:如果黑桃4-2分布时,有没有打成的希望?看来黑桃.红桃.方块各1个失张无法避免? 但希望还是有的,那就是东家拿2张黑桃,并且有3张将牌. 这时庄家可以清2轮将牌,拔2轮黑 ...
- [leetcode] Bitwise AND of Numbers Range
Bitwise AND of Numbers Range Given a range [m, n] where 0 <= m <= n <= 2147483647, return t ...
- Hibernate学习0.Hibernate入门
Hibernate是什么 面向java环境的对象/关系数据库映射工具. 1.开源的持久层框架. 2.ORM(Object/Relational Mapping)映射工具,建立面向对象的域模型和关系数据 ...
- Charles使用详情
Charles各版本下载: Charles for Windows 32 bit Charles for Windows 64 bit Charles for linux Charles for Ma ...