初涉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中是否包含这个字典中 ...
随机推荐
- JS与JQ的对比与提高
来吧, 案例1:先上个例子js写的省市二级联动 <!DOCTYPE html><html> <head> <meta charset="UTF-8& ...
- MyBais中xxMap.xml中的知识点
添加新的mapper的时候下面两个标红的地方要注意,如果统一约定好命名的话都还好,但是就怕一会这样一会那样的业务代码:总之小心! <!-- spring与mybatis整合配置,扫描所有dao, ...
- bzoj1142:[POI2009]Tab
传送门 考虑每次交换都不会改变每个数所在的行和列(不是指编号,而是指和它在同一行或者同一列的数不会发生变化) 由于每个数互不相同,所以记录下每个数所在的行和列,暴力判断就好了 代码: #include ...
- Ubuntu使用实录
在实验室的电脑上重新配置了Linux开发环境,使用的是Ubuntu 14.04.5 LTS. 在开发中遇到的问题甚多,一一记录如下: 1.切换为root身份 先给root用户设定密码,然后进行切换 s ...
- 洛谷1072(gcd的运用)
已知正整数a0,a1,b0,b1,设某未知正整数x满足: 1. x 和 a0 的最大公约数是 a1: 2. x 和 b0 的最小公倍数是b1. Hankson 的“逆问题”就是求出满足条件的正整数 ...
- Tourists Codeforces - 487E
https://codeforces.com/contest/487/problem/E http://uoj.ac/problem/30 显然割点走过去就走不回来了...可以看出题目跟点双有关 有一 ...
- linux 设置固定IP centOS6.5
主要是要把Linux的IP固定下来,可以用另一台机器SSH连接. ping的用法: 基本语法:ping [-选项] IP地址或域名 功能描述:测试网络是否连通 常用选项:-c -c 指定发送数据包的次 ...
- 540 Single Element in a Sorted Array 有序数组中的单一元素
给定一个只包含整数的有序数组,每个元素都会出现两次,唯有一个数只会出现一次,找出这个数.示例 1:输入: [1,1,2,3,3,4,4,8,8]输出: 2 示例 2:输入: [3,3,7,7,10,1 ...
- python学习之列表元组,字典
list:元素性质可以不一致,元素还可以是list,可类似数组方法进行索引(也可以用负数索引,-1表示最后一个),可用.append('')进行动态增加,可用pop()删除最后一个或者pop(i)删除 ...
- ZOJ Seven-Segment Display 暴力dfs + 剪枝
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3954 0 = on 1 = off A seven segment ...