初涉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
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中是否包含这个字典中 ...
随机推荐
- Ajax登陆,使用Spring Security缓存跳转到登陆前的链接
Spring Security缓存的应用之登陆后跳转到登录前源地址 什么意思? 用户访问网站,打开了一个链接:(origin url)起源链接 请求发送给服务器,服务器判断用户请求了受保护的资源. 由 ...
- 002-tomcat安装与配置
1.创建目录 [root@bogon tomcat]#mkdir /usr/local/java/tomcat 2.上传压缩包并解压 [root@bogon tomcat]# tar xvf apac ...
- Mysql 主从(转)
转自 http://blog.csdn.net/hguisu/article/details/7325124
- line-height与图文对齐 笔记
基本概念: 块:block 特点独行 内联:inline 内联块:inline-block 如果元素display属性默认值为block,则为块元素.如div p 如果元素display属性默认值为i ...
- Maven - settings.xml里的offline节点的作用
场景 某天我在本地修改了某个子项目的代码,并进行了打包:mvn clean install -DskipTests,接着我运行父项目却发现自己刚刚的改动并没有生效,或者说,我刚刚打包好的子项目变回了打 ...
- AVL树(自平衡二叉查找树)
了解AVL树之前要先了解二叉查找树(BST),BST查找元素的时间复杂度平均是O(logN),最坏的情况是O(N),所有的元素都接在左子树(或者右子树)就相当于一串链表了.而AVL树会对子树过高的情况 ...
- JDBC | 查询表数据行数
两种方法: 1. "select * from userinfo" 利用ResultSet的last和getRow方法来获得ResultSet的总行数,适用于在查询数据的同时统 ...
- FTP任务(重点看断点续传)
一.FTP任务目录: 1. 多用户同时登陆: socketserver 2. 用户登陆,加密认证: md5加密 3. 上传/下载文件,保证文件一致性:md5摘要 4. 传输过程中现实进度条 5 ...
- 关于gc日志中Desired Survivor的疑问和对象晋升老年代的小结
问题背景 (下面的所有内容都是根据书上的Serial/Serial Old收集器下的情况) 在<深入理解JVM>一书中的——3.6.3长期存活的对象将进入老年代的介绍中, 一个例子的jvm ...
- 116 Populating Next Right Pointers in Each Node 每个节点的右向指针
给定一个二叉树 struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNod ...