AC自动机模板,注意!ch,Fail,lab数组的大小不是n而是节点个数,需要认真计算!

 #include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <queue> using namespace std; int cnt;
int ch[][],Fail[],lab[];
char str[],S[]; void Add(const char * s)
{
int p=,i;
for(i=;s[i];++i)
{
if(!ch[p][s[i]-])ch[p][s[i]-]=++cnt;
p=ch[p][s[i]-];
}
lab[p]++;
return ;
} void Build()
{
int i,t;
queue<int> Q;
Q.push();
while(!Q.empty())
{
t=Q.front();Q.pop();
for(i=;i<;++i)
{
if(ch[t][i])
{
Q.push(ch[t][i]);
Fail[ch[t][i]]=t?ch[Fail[t]][i]:;
}
else ch[t][i]=ch[Fail[t]][i];
}
}
return ;
} int main()
{
int T,n,i; scanf("%d",&T);
while(T--)
{
memset(Fail,,sizeof(Fail));
memset(ch,,sizeof(ch));
memset(lab,,sizeof(lab));
cnt=;
scanf("%d",&n);
for(i=;i<=n;++i)
{
scanf("%s",str+);
Add(str);
}
Build();
scanf("%s",S+);
int p=,Ans=;
for(i=;S[i];++i)
{
p=ch[p][S[i]-];
if(lab[p])
{
int pp=p;
while(pp)Ans+=lab[pp],lab[pp]=,pp=Fail[pp];
}
}
printf("%d\n",Ans);
} return ;
}

[hdu2222] [AC自动机模板] Keywords Search [AC自动机]的更多相关文章

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

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

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

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

  3. 【HDU2222】Keywords Search AC自动机

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

  4. hdu2222 Keywords Search ac自动机

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

  5. Keywords Search(AC自动机模板)

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

  6. HDU2222 Keywords Search —— AC自动机

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

  7. Match:Keywords Search(AC自动机模板)(HDU 2222)

    多模匹配 题目大意:给定很多个字串A,B,C,D,E....,然后再给你目标串str字串,看目标串中出现多少个给定的字串. 经典AC自动机模板题,不多说. #include <iostream& ...

  8. hdu 2222 Keywords Search ac自动机模板

    题目链接 先整理一发ac自动机模板.. #include <iostream> #include <vector> #include <cstdio> #inclu ...

  9. POJ2222 Keywords Search AC自动机模板

    http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:给出一些单词,求多少个单词在字符串中出现过(单词表单词可能有相同的,这些相同的单词视为不同的分别计数 ...

随机推荐

  1. setings.py配置文件详解

    BASE_DIR指的是项目的根目录.SECRET_KEY是安全码. # SECURITY WARNING: don't run with debug turned on in production! ...

  2. 过河 2005年NOIP全国联赛提高组(离散化+dp)

    1105 过河 2005年NOIP全国联赛提高组  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond       题目描述 Description 在河上有一 ...

  3. ubuntu16.04更改源

    最近用apt-get安装软件总是提示列表无法全部更新,导致一些软件安装不上,下面我们通过讲/etc/apt/sources.list里为阿里源,实现访问. 第一步: 备份/etc/apt/source ...

  4. 乐字节-Java8核心特性实战之函数式接口

    什么时候可以使用Lambda?通常Lambda表达式是用在函数式接口上使用的.从Java8开始引入了函数式接口,其说明比较简单:函数式接口(Functional Interface)就是一个有且仅有一 ...

  5. 观察者模式-C#实现

    定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并被自动更新. 观察者模式有四个角色:抽象主题.具体主题.抽象观察者.具体观察者. 抽象主题:把所有观察者对象 ...

  6. Java 集合 ArrayList 需要知道的几个问题

    问:Arraylist 的动态扩容机制是如何自动增加的?简单说说你理解的流程? 答:当在 ArrayList 中增加一个对象时 Java 会去检查 Arraylist 以确保已存在的数组中有足够的容量 ...

  7. POJ 1584 计算几何

    思路: 求一遍凸包 用三角形面积(叉积求一下)/边长 求出来高,跟半径比一比 坑点:凸包上三点共线 //By SiriusRen #include <cmath> #include < ...

  8. Contact

    UF3000: 1.wafer进去prober后,默认probercard不会跟chuck上的wafer接触. 2.通过prober界面上的按钮向上移动,使得prober card和wafer的距离为 ...

  9. Android RecyclerView利用Glide加载大量图片into(Target)导致OOM异常

    学过android的人应该都知道Glide是一个无比强大的图片加载库,它内部已经提供了很好的缓存机制供我们选择,我们只需一个参数调用即可(DiskCacheStrategy()),而不必像Univer ...

  10. 关于桌面程序被安全软件误判为HEUR:Trojan.Win32.Generic的解决方案

    最近写了一个桌面程序,里面用了些读取系统环境变量.提取文件图标.启动外部程序之类的操作. 然后…………卡巴斯基就把它识别成了HEUR:Trojan.Win32.Generic………… 咱遵纪守法好程序 ...