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 ...
随机推荐
- oracle user account locked
1.Question describe when you use account scott/tiger connect to oracle, you will see "the user ...
- [vim] vim入门
1. 概述 工欲善其事 必先利其器.vim是非常好用的文本编辑器,可以将它看作是vi的进阶.绝大多数Unix系统都会内置vi编辑器,vi是文本编辑器,vim是程序编辑器.相比vi,它可以根据文件的类型 ...
- andriod RadioButton
<?xml version="1.0" encoding="UTF-8"?> <LinearLayout android:orientatio ...
- 转:【前端福利】用grunt搭建自动化的web前端开发环境-完整教程
原文地址:http://blog.csdn.net/wangfupeng1988/article/details/46418203 jQuery在使用grunt,bootstrap在使用grunt,百 ...
- 如何启动或关闭oracle的归档(ARCHIVELOG)模式
参考文献: http://www.eygle.com/archives/2004/10/oracle_howtoeci.html 1.管理员身份连接数据库 C:\Users\Administrator ...
- Android 在外部存储读写文件
本文主要介绍android中如何在外部存储读写数据 sd卡的路径 sdcard:2.3之前的sd卡路径 mnt/sdcard:4.3之前的sd卡路径 storage/sdcard:4.3之后的sd卡路 ...
- .net程序员工作两年总结
(2015年9月) 最近换了工作,面试了很多家公司想总结下,以便以后回顾知道自己是怎么走过来的. 入行背景: 我是半路转行做软件开发的,2011年7月大学专科毕业,大学专业是:机械制造及其自动化:20 ...
- centos查看硬件信息
服务器硬件信息常见, 是经常要操作的事: 下面分享一些常见的硬件查看命令: 1.centos 下查看硬件信息内容非常全面. CentOS常用命令查看cpu more /proc/cpuinfo 2.C ...
- OOD沉思录 --- 类和对象的关系 --- 包含关系3
4.7 类包含的对象数目不应当超过开发者短期记忆数量,这个数目通常应该是6左右 4.8 让系统在窄而深的包含体系中垂直分布 假设有如下两份菜单: 正餐 --->甜瓜 --->牛排 ---& ...
- 未能找到元数据文件“引用的DLL的路径”
使用VS的时候 偶尔会出现错误 [未能找到元数据文件“引用的DLL的路径”] 但是实际上项目中这些DLL都是做了引用的,甚至你前一天打开还是好好的,睡一觉起来 不知道什么原因 就酱紫了 原因:不详 ...