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

    贝茜正在领导奶牛们逃跑.为了联络,奶牛们互相发送秘密信息.
    信息是二进制的,共有M(1≤M≤50000)条.反间谍能力很强的约翰已经部分拦截了这些信息,知道了第i条二进制信息的前bi(l《bi≤10000)位.他同时知道,奶牛使用N(1≤N≤50000)条密码.但是,他仅仅了解第J条密码的前cj(1≤cj≤10000)位.
    对于每条密码J,他想知道有多少截得的信息能够和它匹配.也就是说,有多少信息和这条密码有着相同的前缀.当然,这个前缀长度必须等于密码和那条信息长度的较小者.
    在输入文件中,位的总数(即∑Bi+∑Ci)不会超过500000.

Input

    第1行输入N和M,之后N行描述秘密信息,之后M行描述密码.每行先输入一个整数表示信息或密码的长度,之后输入这个信息或密码.所有数字之间都用空格隔开.

Output

    共M行,输出每条密码的匹配信息数.

题目分析

那么显然就是一个前缀计数。分成两类:其他是它前缀;它是其他前缀。
 #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

输入数据的第一行包含一个整数N,表示数组中的元素个数。
第二行包含N个整数A1,A2,…,AN。

Output

输出一行包含给定表达式可能的最大值。

Sample Input

5
1 2 3 1 2

Sample Output

6

HINT

满足条件的(l1,r1,l2,r2)有:(1,2,3,3),(1,2,4,5),(3,3,4,5)。
对于100%的数据,2 ≤ N ≤ 4*105,0 ≤ Ai ≤ 109。

题目分析

这题就是要稍加建模的题了。

若用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

最新文章

  1. day25、 静态属性、类方法、静态方法、组合、继承、
  2. sevice__属性介绍: android:exported
  3. Linq To Nhibernate 性能优化(入门级)
  4. 标准MDL方法修改Page、NonPage内存的属性
  5. Qt之QAbstractItemView视图项拖拽(一)
  6. 使用Assetbundle时可能遇到的坑
  7. 查看Linux声卡基本信息[转载]
  8. webStorm破解
  9. SpringBoot+ Mybatis 搭建
  10. FFmpeg开发实战(五):FFmpeg 抽取音视频的视频数据
  11. P5290 [十二省联考2019]春节十二响
  12. 在Linux CentOS6系统中安装开源CMS程序OpenCart的教程
  13. kali系统固化到固态硬盘小记(赠送给广大折腾党的笔记)
  14. python - 自定制property/property的延时计算
  15. 1-3-编译Linux内核
  16. sql server 删除所有表、视图、存储过程
  17. sql中 substring和charindex 的用法
  18. android 获取屏幕高度和宽度 的方法
  19. Castle连接多数据库配置
  20. 分享基于Sails.js和React.js的全栈聊天室

热门文章

  1. php7+新特性
  2. python tickle模块与json模块
  3. linux 设置固定IP centOS6.5
  4. UESTC - 1544 当咸鱼也要按照基本法 组合数学 容斥原理
  5. 牛客网Java刷题知识点之Java为什么不能支持多继承,但可以用接口来间接实现多继承
  6. 表单辅助函数-form_open()
  7. Linux网路命令netstat
  8. mirror.js 整合redux的好工具
  9. 连接sql server、插入数据、从数据库获取时间(C#)
  10. 从零开始利用vue-cli搭建简单音乐网站(五)