Trie树 模板
普通Trie:
struct TRIE{
int trie[MAXN][],tot,end[MAXN];
TRIE(){tot=;}
void insert(char *s){//s为要插入的字符串
int len=strlen(s);
int u=;//1为根节点
for(int i=;i<len;i++){
int c=s[i]-'a';//'a'有时需换成'A'或'0'
if(!trie[u][c])//没有共同前缀,建立一个新的
trie[u][c]=++tot;//tot为总点数
u=trie[u][c];//继续向下插入单词
}
end[u]=true;//标记是一个出现过的单词(图中涂红色)
}
//查找单词是否是词典中某单词的前缀
bool find1(char *s){//s为要查找的字符串
int len=strlen(s);
int u=;//1为根节点
for(int i=;i<len;i++){
int c=s[i]-'a';//'a'有时需换成'A'或'0'
if(!trie[u][c])//单词没有出现,直接返回false
return false;
u=trie[u][c];//继续向下查找单词
}
//如果扫描完了这个单词
return true;//是某个单词的前缀
}
//查找单词是否在词典中出现过
bool find2(char *s){//s为要查找的字符串
int len=strlen(s);
int u=;//1为根节点
for(int i=;i<len;i++){
int c=s[i]-'a';//'a'有时需换成'A'或'0'
if(!trie[u][c])//单词没有出现,直接返回false
return false;
u=trie[u][c];//继续向下查找单词
}
//如果扫描完了这个单词
return end[u];//如果出现过,返回true;如果没有出现过(是前缀),返回false
}
}Trie;
01Trie:
struct TRIE_01{
int trie[MAXN*32][2],tot,end[MAXN*32];
TRIE_01(){tot=1;}
void insert(int x){
int root=1;
for(int i=32;i>=0;i--){
int t=((x>>i)&1);
if(!trie[root][t]) trie[root][t]=++tot;
root=trie[root][t];
}
end[root]=x;
}
int query(int x){ //查询所有数中和 x异或结果最大的数
int root=1;
for(int i=32;i>=0;i--){
int v=(x>>i)&1;
//利用贪心策略,优先寻找和当前位不同的数
if(trie[root][v^1]) root=trie[root][v^1];
else root=trie[root][v];
}
return end[root]; //返回结果
}
}Trie_01;
它还可以有平衡树的作用:
题目就是普通平衡树
#include <cstdio>
#include <algorithm>
#include <cstring>
#define setIO(s) freopen(s".in","r",stdin)
#define maxn 100010 * 33
using namespace std;
int root=1,tot=1,sumv[maxn],n,opt,x,ch[maxn][2];
void ins(int val,int c){
val += (int)1e7;
for(int i=31,p=root,t;i>=0;--i){
t=(val>>i)&1;
if(!ch[p][t]) ch[p][t]=++tot;
p=ch[p][t]; sumv[p]+=c;
}
}
int rank(int val){
val += (int)1e7;
int res=0,p=root;
for(int i=31;i>=0;--i){
int t=(val>>i)&1;
if(t) res += sumv[ch[p][0]];
p=ch[p][t];
}
return res; struct TRIE_01{
int trie[MAXN*32][2],tot,end[MAXN*32];
TRIE_01(){tot=1;}
void insert(int x){
int root=1;
for(int i=32;i>=0;i--){
int t=((x>>i)&1);
if(!trie[root][t]) trie[root][t]=++tot;
root=trie[root][t];
}
end[root]=x;
}
int query(int x){ //查询所有数中和 x异或结果最大的数
int root=1;
for(int i=32;i>=0;i--){
int v=(x>>i)&1;
//利用贪心策略,优先寻找和当前位不同的数
if(trie[root][v^1]) root=trie[root][v^1];
else root=trie[root][v];
}
return end[root]; //返回结果
}
}Trie_01; }
int kth(int val){
int k=root,res=0;
for(int i=31;i>=0;--i){
if(val>sumv[ch[k][0]]) res|=(1<<i),val-=sumv[ch[k][0]],k=ch[k][1];
else k=ch[k][0];
}
res-=(1e7); return res;
}
int main(){
//setIO("input");
scanf("%d",&n);
while(n--){
scanf("%d%d",&opt,&x);
if(opt==1) ins(x,1);
else if(opt==2) ins(x,-1);
else if(opt==3) printf("%d\n",rank(x)+1);
else if(opt==4) printf("%d\n",kth(x));
else if(opt==5) printf("%d\n",kth(rank(x)));
else if(opt==6) printf("%d\n",kth(rank(x+1)+1));
}
return 0;
}
Trie树 模板的更多相关文章
- HDU 1251 Trie树模板题
1.HDU 1251 统计难题 Trie树模板题,或者map 2.总结:用C++过了,G++就爆内存.. 题意:查找给定前缀的单词数量. #include<iostream> #incl ...
- Phone list(Trie树模板)
Phone List 共t组数据,给定n个长度不超过10的字符串,问其中是否存在两个数S,T,使得S是T的前缀. 存在则输出NO,不存在输出YES 输入样例#1: 2 3 911 97625999 9 ...
- poj3630 Phone List (trie树模板题)
Phone List Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 26328 Accepted: 7938 Descr ...
- HDU 1251 统计难题 (Trie树模板题)
题目链接:点击打开链接 Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单 ...
- Trie树模板1字符串统计
Trie树模板1字符串统计 我们首先来了解一下字典树,首先看一下一张字典树的图片 字典树就是一个可以高效存储.查找字符串的树,比如上面这个字典树就是存储abc,acb,bac的字典树. 1.插入操作( ...
- hiho #1014 : Trie树(模板)
Trie树 [题目链接]Trie树 &题意: 输入 输入的第一行为一个正整数n,表示词典的大小,其后n行,每一行一个单词(不保证是英文单词,也有可能是火星文单词哦),单词由不超过10个的小写英 ...
- LightOJ 1129 - Consistency Checker Trie树模板
题意:给出n条串判断是否存在一个串为另一个串的前缀. 思路:套Trie树的模板,先全部插入,再查找每个字串,如果查找字串完毕,但还存在下一个节点,说明存在前缀. /** @Date : 2016-11 ...
- Trie树模板2
Trie数模板2 problem 这道题然后我们求最大异或对,我们很容易想出来 \(O(n^2)\) 的做法,两层循环遍历搞定 然后我们知道这样是肯定是肯定过不了的,我们考虑用字典树解决,然后我们来看 ...
- trie树模板(统计难题)
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submi ...
- Trie树模板~~~
* + ; ; // 字母表为全体小写字母的Trie struct Trie { int ch[maxnode][sigma_size]; int val[maxnode]; int sz; // 结 ...
随机推荐
- 概率dp——cf518D
通过最后的概率求最终的期望 #include<bits/stdc++.h> using namespace std; ; double p,dp[maxn][maxn]; int n,t; ...
- NOI2014
听说14,15年的题是最简单的,然后除了提答以外的不那么水的题都是以前讲过.做过的,都比较好想到,但是我实现起来却有各种Bug,也完全不能在5h里AC...太弱了 [NOI2014]起床困难综合症 纯 ...
- tp5 mkdir() 没有权限
- Visual Studio上开发Python六大功能
Visual Studio上开发Python六大功能 一.整合 Python 直译器 (Interpreter) & 互动视窗 (Interactive) Visual Studio 高度整合 ...
- System.Web.Mvc.IActionFilter.cs
ylbtech-System.Web.Mvc.IActionFilter.cs 1.程序集 System.Web.Mvc, Version=5.2.3.0, Culture=neutral, Publ ...
- PAT甲级——A1125 Chain the Ropes【25】
Given some segments of rope, you are supposed to chain them into one rope. Each time you may only fo ...
- substring常用的两种方法
1.public String substring(int beginIndex, int endIndex) 第一个参数int为开始的索引,对应String数字中的开始位置, 第二个参数是截止的索引 ...
- mybatis-plus分页查询
在springboot中整合mybatis-plus 按照官方文档进行的配置:快速开始|mybatis-plus 引入依赖: <!-- 引入mybatisPlus --> <depe ...
- 6_4.springboot2.x数据整合springData介绍
介绍 Spring Data 项目的目的是为了简化构建基于Spring 框架应用的数据访问技术,包括非关系数据库.Map-Reduce 框架.云数据服务等等:另外也包含对关系数据库的访问支持. spr ...
- js 获取自定义属性值
html: <p tid="1" onClick="change()">111</p> <p tid="2" ...