【AC自动机】hdu2222 Keywords Search
AC自动机模板题,给你n个模式串和一个文本串,问你有几个模式串在文本串出现过。
注意防止重复统计
这里推荐一波郭大爷的介绍,简单易懂。
http://www.bilibili.com/video/av6295004/
这个视频里的hdu2222代码好像有点问题,我现在这份代码已经更改。
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
queue<int>q;
int child[500010][26],fail[500010],cnts[500010],size;
void Insert(char S[])
{
int len=strlen(S);
int now=0;
for(int i=0;i<len;++i)
{
if(!child[now][S[i]-'a'])
child[now][S[i]-'a']=size++;
now=child[now][S[i]-'a'];
}
if(cnts[now]==-1)
cnts[now]=0;
++cnts[now];
}
void build()
{
fail[0]=-1;
q.push(0);
while(!q.empty())
{
int U=q.front(); q.pop();
for(int i=0;i<26;++i)
if(child[U][i])
{
int V=fail[U];
while(V!=-1)
{
if(child[V][i])
{
fail[child[U][i]]=child[V][i];
break;
}
V=fail[V];
}
if(V==-1)
fail[child[U][i]]=0;
if(cnts[fail[child[U][i]]]!=-1 && cnts[child[U][i]]==-1)
cnts[child[U][i]]=0;
q.push(child[U][i]);
}
}
}
int calc(int U)//˳×ÅfailÖ¸Õë»ØÈ¥£¬¿´Í³¼Æµ½Á˼¸¸ö´®
{
int res=0;
while(U)
{
if(cnts[U]==-1)
break;
res+=cnts[U];
cnts[U]=-1;//·ÀÖ¹ÖØ¸´Í³¼Æ
U=fail[U];
}
return res;
}
int match(char S[])
{
int len=strlen(S);
int res=0,now=0;
for(int i=0;i<len;++i)
{
if(child[now][S[i]-'a'])
now=child[now][S[i]-'a'];
else
{
int U=fail[now];
while(U!=-1 && child[U][S[i]-'a']==0)
U=fail[U];
if(U==-1)
now=0;
else
now=child[U][S[i]-'a'];
}
if(cnts[now]!=-1)
res+=calc(now);
}
return res;
}
void Init()
{
memset(child,0,sizeof(child));
memset(fail,0,sizeof(fail));
memset(cnts,-1,sizeof(cnts));
size=1;
}
int T,n;
char s[1000010];
int main()
{
//freopen("hdu2222.in","r",stdin);
scanf("%d",&T);
for(;T;--T)
{
Init();
scanf("%d",&n);
for(int i=1;i<=n;++i)
{
scanf("%s",s);
Insert(s);
}
build();
scanf("%s",s);
printf("%d\n",match(s));
}
return 0;
}
【AC自动机】hdu2222 Keywords Search的更多相关文章
- [hdu2222] [AC自动机模板] Keywords Search [AC自动机]
AC自动机模板,注意!ch,Fail,lab数组的大小不是n而是节点个数,需要认真计算! #include <iostream> #include <algorithm> #i ...
- AC自动机(Keywords Search)
题目链接:https://cn.vjudge.net/contest/280743#problem/A 题目大意:首先给你T组测试样例,然后给你n个字符串,最后再给你一个模式串,然后问你这一些字符串中 ...
- [AC自动机模板]Keywords Search
只是记录一下代码 AC自动机算法的教程请移步这里 还有这里 指针看着懵逼的还可以看一下这里 #include<iostream> #include<cstdio> #inclu ...
- 【AC自动机】Keywords Search
[题目链接] https://loj.ac/problem/10057 [题意] 原题来自:HDU 2222 给定 n 个长度不超过 50 的由小写英文字母组成的单词准备查询,以及一篇长为 m 的文 ...
- HDU2222 Keywords Search 【AC自动机】
HDU2222 Keywords Search Problem Description In the modern time, Search engine came into the life of ...
- hdu2222 Keywords Search ac自动机
地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=2222 题目: Keywords Search Time Limit: 2000/1000 MS ...
- HDU2222 Keywords Search [AC自动机模板]
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- HDU2222 Keywords Search(AC自动机)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- hdu2222 Keywords Search【AC自动机】
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- HDU2222 Keywords Search —— AC自动机
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 Keywords Search Time Limit: 2000/1000 MS (Java/O ...
随机推荐
- The 13th Zhejiang Provincial Collegiate Programming Contest - I
People Counting Time Limit: 2 Seconds Memory Limit: 65536 KB In a BG (dinner gathering) for ZJU ...
- [SDOI2011]消防/[NOIP2007] 树网的核
消防 题目描述 某个国家有n个城市,这n个城市中任意两个都连通且有唯一一条路径,每条连通两个城市的道路的长度为zi(zi<=1000). 这个国家的人对火焰有超越宇宙的热情,所以这个国家最兴旺的 ...
- Virtualization solutions on Linux systems - KVM and VirtualBox
Introduction Virtualization packages are means for users to run various operating systems without &q ...
- 3中转换JSON数据的方式
一:前言 来公司一个星期,把最近做的东西梳理下,并把觉得有必要的知识点记载下,现在传数据很多都是用JSON来传数据,所以我就找了集中传json的方式,其实是有五种的,但是有一个我没有用过,太陌生了,上 ...
- 数据结构&图论:K短路-可持久化可并堆
本来A*就可以搞定的题,为了怕以后卡复杂度,找了个这么个方法 现阶段水平不够就不补充算法分析部分了 对于图G,建立一个以终点t为起点的最短路径构成的最短路径树 (就是反着跑一遍最短路,然后对于一个不为 ...
- BZOJ 4527: K-D-Sequence
4527: K-D-Sequence Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 163 Solved: 66[Submit][Status][D ...
- python3 线程_threading模块
'''并发:同一个时间段内运行多个程序的能力 进程就是一个程序在一个数据集上的一次动态执行过程.进程一般由程序.数据集.进程控制块三部分组成 程序:食谱数据集:鸡蛋.牛奶.糖等进程控制块:记下食谱做到 ...
- JavaScript的条件运算符与条件语句
1.条件运算符 比较运算符 == 判断左右两边数据的值是否相等 === 判断左右两边数据的之是否相等,同时还判断两边的数据类型是否一样 != 比较运算符的比较结果都是布尔值,true或者f ...
- UVALive - 5844
题是pdf版 Sample Input23mississippinni55i55ippi2foobar|=o08arSample Output10 /** 题意:给出一个normal串,一个leet串 ...
- Activiti 6.0 入门篇
从Activiti网站下载Activiti UI WAR文件(或百度云) 将下载的activiti-app.war复制到Tomcat的webapps目录. 启动Tomcat 打开浏览器并转到 http ...