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的更多相关文章

  1. [hdu2222] [AC自动机模板] Keywords Search [AC自动机]

    AC自动机模板,注意!ch,Fail,lab数组的大小不是n而是节点个数,需要认真计算! #include <iostream> #include <algorithm> #i ...

  2. AC自动机(Keywords Search)

    题目链接:https://cn.vjudge.net/contest/280743#problem/A 题目大意:首先给你T组测试样例,然后给你n个字符串,最后再给你一个模式串,然后问你这一些字符串中 ...

  3. [AC自动机模板]Keywords Search

    只是记录一下代码 AC自动机算法的教程请移步这里 还有这里 指针看着懵逼的还可以看一下这里 #include<iostream> #include<cstdio> #inclu ...

  4. 【AC自动机】Keywords Search

    [题目链接] https://loj.ac/problem/10057 [题意] 原题来自:HDU 2222 给定  n 个长度不超过 50 的由小写英文字母组成的单词准备查询,以及一篇长为 m 的文 ...

  5. HDU2222 Keywords Search 【AC自动机】

    HDU2222 Keywords Search Problem Description In the modern time, Search engine came into the life of ...

  6. hdu2222 Keywords Search ac自动机

    地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=2222 题目: Keywords Search Time Limit: 2000/1000 MS ...

  7. HDU2222 Keywords Search [AC自动机模板]

    Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others ...

  8. HDU2222 Keywords Search(AC自动机)

    Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others ...

  9. hdu2222 Keywords Search【AC自动机】

    Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others ...

  10. HDU2222 Keywords Search —— AC自动机

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 Keywords Search Time Limit: 2000/1000 MS (Java/O ...

随机推荐

  1. zigbee芯片 - JN5169

    http://www.nxp.com/products/wireless-connectivity/2.4-ghz-wireless-solutions/support-resources-for-j ...

  2. <video>标签的特性

    以前的网页视频 过去还没有HTML5的时候,我们处理网页视频的时候,都是通过Flash插件来实现的,然而,并非所有浏览器都有同样的插件. 现在有了HTML5带来的video元素,开发者能够很方便地将视 ...

  3. SpringMVC——说说视图解析器

    学习SpringMVC——说说视图解析器   各位前排的,后排的,都不要走,咱趁热打铁,就这一股劲我们今天来说说spring mvc的视图解析器(不要抢,都有位子~~~) 相信大家在昨天那篇如何获取请 ...

  4. css之postion定位

    css position之初识 我们先看看position定位有那几个属性,下面是我从w3c的截图,为我们认识定位做初步的了解 通过上面这张图嘞,我们知道了position有5个属性,那么下面,我就来 ...

  5. svn“Previous operation has not finished; run 'cleanup' if it was interrupted“ 或者不能cleanup,或者提示空目录 报错的解决方法

    参考了文档: http://blog.csdn.net/superch0054/article/details/38668017 今天碰到了个郁闷的问题,svn执行clean up命令时报错“Prev ...

  6. bzoj 1706: [usaco2007 Nov]relays 奶牛接力跑——倍增floyd

    Description FJ的N(2 <= N <= 1,000,000)头奶牛选择了接力跑作为她们的日常锻炼项目.至于进行接力跑的地点 自然是在牧场中现有的T(2 <= T < ...

  7. python基础===取txt文件的若干行到另一个文件

    #取txt文件 的若干行到另一个txt f1 = open(r'F:\movie.txt','rb') f2= open(r'F:\movie2.txt','ab') i=0 while True: ...

  8. linux下面某些常用命令的用法【转】

    转自:http://blog.csdn.net/luo3532869/article/details/7584290 ls 命令用于常看目录,用法:ls [选项][目录或文件]例:使用ls命令显示/h ...

  9. Javascript制作放大镜

    方法一 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8 ...

  10. java中final修饰符的使用

    1.final修饰符的用法: final可以修饰变量,被final修饰的变量被赋初始值之后,不能对它重新赋值. final可以修饰方法,被final修饰的方法不能被重写. final可以修饰类,被fi ...