【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 ...
随机推荐
- tomcat发布web项目的三种方式
tomcat发布web项目的三种方式 方式一: 配置tomcat 安装目录下的conf/server.xml <Host name="loaclhost">标签里面添加 ...
- AWS文档与用户指南
AWS Command Line Interface http://docs.amazonaws.cn/cli/latest/userguide/cli-chap-welcome.html VM Im ...
- ubuntu12.04 Qt WebKit编译
转载自:http://my.oschina.net/u/257674/blog/167050 官方文档: http://trac.webkit.org/wiki/BuildingQtOnLinux#D ...
- notepad++中快速插入当前时间方法
转载自:http://blog.csdn.net/donghustone/article/details/7436483 在notepad++中快速插入当前时间方法: 插件是notepad++的一大优 ...
- final 的作用
1.修饰类 类不能被继承 2.修饰方法 目的有二: 1)禁止子类重写该方法 2)执行效率(JVM相关的东西,不用太关注) 3.修饰变量 final修饰原始类型的变量,该变量不能被修改 final修饰引 ...
- [object-c 2.0 程序设计]object-c review (一)
// // main.m // cmdTry // // Created by Calos Chen on 2017/8/21. // Copyright © 2017年 Calos Chen. Al ...
- [bzoj4518][Sdoi2016]征途-斜率优化
Brief Description Pine开始了从S地到T地的征途. 从S地到T地的路可以划分成n段,相邻两段路的分界点设有休息站. Pine计划用m天到达T地.除第m天外,每一天晚上Pine都必须 ...
- 转:在android中button响应的两种方式
1. 在布局文件中添加button的监听名字 Android:onClick="buttonOnClick" 例如: <Button android:id="@+i ...
- 内存管理相关函数 -- Linux【转】
转自:http://blog.csdn.net/cy_cai/article/details/47001245 1.kmalloc()/kfree() static __always_inline v ...
- wait(),sleep(),notify(),join()
wait()注意以下几点: 1)wait()是属于Object类的方法. 2)调用了wait()之后会引起当前线程处于等待状态. 3)将当前线程置入“预执行队列”中,并且在wait()所在的代码行处停 ...