初涉trie
trie:字符串算法中的重要“数据结构”
什么是trie
trie就是利用字符串的公共前缀所建成的树。

众所周知树是有很多很好的性质的,于是trie可以结合其他知识点做一些有趣的事情。
trie的例题
注意
trie的题一般数组开成$f[lensSum][size]$,其中$lensSum$是所有字符串的长度之和,$size$是字符集大小。
【判断前缀】poj3630Phone List
Description
Given a list of phone numbers, determine if it is consistent in the sense that no number is the prefix of another. Let's say the phone catalogue listed these numbers:
- Emergency 911
- Alice 97 625 999
- Bob 91 12 54 26
In this case, it's not possible to call Bob, because the central would direct your call to the emergency line as soon as you had dialled the first three digits of Bob's phone number. So this list would not be consistent.
Input
The first line of input gives a single integer, 1 ≤ t ≤ 40, the number of test cases. Each test case starts with n, the number of phone numbers, on a separate line, 1 ≤ n ≤ 10000. Then follows n lines with one unique phone number on each line. A phone number is a sequence of at most ten digits.
Output
For each test case, output "YES" if the list is consistent, or "NO" otherwise.
Sample Input
2
3
911
97625999
91125426
5
113
12340
123440
12345
98346
Sample Output
NO
YES
题目分析
这个是trie最基础的应用——判断前缀。
#include<cstdio>
#include<cctype>
#include<cstring>
const int maxn = ;
const int maxe = ; int tt,n,tot;
int f[maxn][maxe];
char ch[maxe];
bool vis[maxn],fl; int read()
{
char ch = getchar();
int num = ;
bool fl = ;
for (; !isdigit(ch); ch = getchar())
if (ch=='-') fl = ;
for (; isdigit(ch); ch = getchar())
num = (num<<)+(num<<)+ch-;
if (fl) num = -num;
return num;
}
void insert(char *s)
{
int n = strlen(s), rt = ;
for (int i=; i<n; i++)
{
int w = s[i]-'';
if (!f[rt][w]) f[rt][w] = ++tot;
else if (i==n-) fl = ;
rt = f[rt][w];
if (vis[rt]) fl = ;
}
vis[rt] = ;
}
int main()
{
tt = read();
while (tt--)
{
memset(vis, , sizeof vis);
memset(f, , sizeof f);
fl = , tot = , n = read();
for (int i=; i<=n; i++)
{
scanf("%s",ch);
insert(ch);
}
printf("%s\n",!fl?"YES":"NO");
}
return ;
}
【前缀统计】bzoj1590: [Usaco2008 Dec]Secret Message 秘密信息
Description
Input
Output
题目分析
#include<bits/stdc++.h>
const int maxn = ;
const int maxe = ;
const int maxNode = ; struct node
{
int end,pass;
}a[maxNode];
int f[maxNode][maxe];
int n,m,lens,tot,r[maxn]; int read()
{
char ch = getchar();
int num = ;
bool fl = ;
for (; !isdigit(ch); ch = getchar())
if (ch=='-') fl = ;
for (; isdigit(ch); ch = getchar())
num = (num<<)+(num<<)+ch-;
if (fl) num = -num;
return num;
}
void insert()
{
int u = ;
for (int i=; i<=lens; i++)
{
if (!f[u][r[i]]) f[u][r[i]] = ++tot;
u = f[u][r[i]];
a[u].pass++;
}
a[u].pass--, a[u].end++;
}
void query()
{
int cnt = , u = ;
for (int i=; i<=lens; i++)
{
u = f[u][r[i]];
if (!u) break;
cnt += a[u].end;
}
cnt += a[u].pass;
printf("%d\n",cnt);
}
int main()
{
tot = , n = read(), m = read();
for (int i=; i<=n; i++)
{
lens = read();
for (int j=; j<=lens; j++) r[j] = read();
insert();
}
for (int i=; i<=m; i++)
{
lens = read();
for (int j=; j<=lens; j++) r[j] = read();
query();
}
return ;
}
【xor最值】bzoj4260: Codechef REBXOR
Description

.jpg)
Input
Output
Sample Input
1 2 3 1 2
Sample Output
HINT
题目分析
这题就是要稍加建模的题了。
若用dp的思想:$l[i]$表示$1≤l≤r≤i$的最大异或和,$r[i]$表示$i≤l≤r≤n$的最大异或和,那么有$ans=max\{l[i]+r[i+1]\}$。
问题就在于求$l[i],r[i]$,这里讨论$l[i]$的求法,$r[i]$同理。若$r!=i$,那么$l[i]=l[i-1]$;否则就是固定了右端点,再找一个左端点使得$a[x]~a[i]$异或和最大。
粗看xor是没有前缀和加法性质的。但是这不就等于在一个集合里找一个数求其与给定数最大的异或和吗?这就转化成为trie的另一个经典应用了。
#include<bits/stdc++.h>
const int maxn = ;
const int maxe = ; int n,tot,cnt,ans;
int f[maxn<<][maxe],a[maxn];
int l[maxn],r[maxn]; int read()
{
char ch = getchar();
int num = ;
bool fl = ;
for (; !isdigit(ch); ch = getchar())
if (ch=='-') fl = ;
for (; isdigit(ch); ch = getchar())
num = (num<<)+(num<<)+ch-;
if (fl) num = -num;
return num;
}
void insert(int x)
{
int u = ;
for (int i=<<; i; i>>=)
{
int c = (x&i)?:;
if (!f[u][c]) f[u][c] = ++tot;
u = f[u][c];
}
}
int find(int x)
{
int u = , ret = ;
for (int i=<<; i; i>>=)
{
int c = (x&i)?:;
if (f[u][c])
ret += i, u = f[u][c];
else u = f[u][-c]; //这里u打成c调了半小时……
}
return ret;
}
int main()
{
n = read();
for (int i=; i<=n; i++) a[i] = read();
memset(f, , sizeof f);
tot = , cnt = , insert();
for (int i=; i<=n; i++)
{
cnt ^= a[i];
insert(cnt);
l[i] = std::max(l[i-], find(cnt));
}
memset(f, , sizeof f);
tot = , cnt = , insert();
for (int i=n; i>=; i--)
{
cnt ^= a[i];
insert(cnt);
r[i] = std::max(r[i+], find(cnt));
}
for (int i=; i<n; i++)
ans = std::max(ans, l[i]+r[i+]);
printf("%d\n",ans);
return ;
}
END
初涉trie的更多相关文章
- NOIP2018 - 暑期博客整理
暑假写的一些博客复习一遍.顺便再写一遍或者以现在的角度补充一点东西. 盛暑七月 初涉基环外向树dp&&bzoj1040: [ZJOI2008]骑士 比较经典的基环外向树dp.可以借鉴的 ...
- 基于trie树做一个ac自动机
基于trie树做一个ac自动机 #!/usr/bin/python # -*- coding: utf-8 -*- class Node: def __init__(self): self.value ...
- 基于trie树的具有联想功能的文本编辑器
之前的软件设计与开发实践课程中,自己构思的大作业题目.做的具有核心功能,但是还欠缺边边角角的小功能和持久化数据结构,先放出来,有机会一点点改.github:https://github.com/chu ...
- [LeetCode] Implement Trie (Prefix Tree) 实现字典树(前缀树)
Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...
- hihocoder-1014 Trie树
hihocoder 1014 : Trie树 link: https://hihocoder.com/problemset/problem/1014 题意: 实现Trie树,实现对单词的快速统计. # ...
- 【BZOJ-2938】病毒 Trie图 + 拓扑排序
2938: [Poi2000]病毒 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 609 Solved: 318[Submit][Status][Di ...
- Poj The xor-longest Path 经典题 Trie求n个数中任意两个异或最大值
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 5646 Accepted: 1226 Description In an ...
- 二分+DP+Trie HDOJ 5715 XOR 游戏
题目链接 XOR 游戏 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...
- 【hihoCoder】1036 Trie图
题目:http://hihocoder.com/problemset/problem/1036 给一个词典dict,词典中包含了一些单词words.要求判断给定的一个文本串text中是否包含这个字典中 ...
随机推荐
- python 的 数据类型
数据类型 1.1整型int py2中有int有范围,超出自动转为long长整型,py3中只有int 整除py2中无小数,可加一行代码实现 十进制转二进制 print(bin(10))十进制转八进制 p ...
- CZGL.AliIoTClient 文档:说明
文档目录: 说明 1. 连接阿里云物联网 2. IoT 客户端 3. 订阅Topic与响应Topic 4. 设备上报属性 4.1 上报位置信息 5. 设置设备属性 6. 设备事件上报 7. 服务调用 ...
- 阿里云物联网 .NET Core 客户端 | CZGL.AliIoTClient:1. 连接阿里云物联网
文档目录: 说明 1. 连接阿里云物联网 2. IoT 客户端 3. 订阅Topic与响应Topic 4. 设备上报属性 4.1 上报位置信息 5. 设置设备属性 6. 设备事件上报 7. 服务调用 ...
- XCode5 编译ffmpeg流程
今天想在Mac10.9 XCode5的环境下把ffmpeg编译出来,本来以为很好搞定,结果版本一升级,以前的命令行不通了,出各种错误.折腾了三四个小时,最后终于成功了. 编译流程: 1.ffmpeg官 ...
- 在Ubuntu14.04 64位上安装Clion
1.下载Clion 1.1 下载Linux版Clion的.tar.gz的压缩包 Clion 2017.3.1 下载安装:https://www.jetbrains.com/clion/download ...
- windows安装redis和PHP redis扩展
1.安装Redis (1)这里选择在github官网上下载Redis,地址:Redis下载地址 下载压缩包(如下图),并解压到本地目录,我放在D:\wamp\redis\redis-windows ( ...
- qq登录,新浪微博登录 ,接口开发
给linux命令在线中文手册加了,qq登录和新浪微博登录,认证用的是auth2.0,并且用了js api和php api相结合的方式来做的.个人觉得这种方式,兼顾安全和人性化.以前写过一篇关于申请的博 ...
- hdu1754I Hate It(splay)
链接 线段树的水题,拿来学习一下splay. 本题涉及到求最大值以及单点更新,折腾了许久,差不多把splay搞明白了. 按位置建树,按位置是一颗排序二叉树,对于区间的操作非常方便,每次操作都将需要的结 ...
- arcgis mdb 数据中的shp 如何合并一起
如上操作 一直往下就可以啦 选择数据源 就可以了,然后就可以load 其他数据啦 ,坐标系要一直
- 爬虫requests库的基本用法
需要注意的几个点: 1.后面的s是一个虚拟目录 2.url后面不用加问号,发起请求的时候会自动帮你加上问号 get_url = 'http://www.baidu.com/s' 3. url的特性:u ...