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. Django-CKeditor使用笔记

    1. 安装django-ckeditor $ pip install django-ckeditor 2. 在setting中,添加ckeditor , ckeditor_uploader 到INST ...

  2. codevs3728联合权值(LCA)

    3728 联合权值  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解  查看运行结果     题目描述 Description 输入描述 Input Des ...

  3. P4280 [AHOI2008]逆序对

    传送门 有一个不会证明的贪心:从左到右考虑每一个位置,然后在每一个位置都贪心选取能让该位置构成的逆序对最少的数.判断逆序对的话只要记一下前缀小于等于某数的总数和后缀小于等于某数的总数就行了 //min ...

  4. Vue.js中学习使用Vuex详解

    在SPA单页面组件的开发中 Vue的vuex和React的Redux 都统称为同一状态管理,个人的理解是全局状态管理更合适:简单的理解就是你在state中定义了一个数据之后,你可以在所在项目中的任何一 ...

  5. Python细节(一)深浅拷贝

    深浅拷贝 只要涉及拷贝,就会涉及创建新对象 浅拷贝,会创建一个新的容器,列表中的元素和原列表的元素用的是同一个内存空间 第一种方法:从头切到尾,完整的复制一份 lst = [1,2,3,4] lst1 ...

  6. jmeter 3.x plugins 的使用

    JMeter Plugins 一直以来,JMeter Plugins为我们提供了很多高价值的JMeter插件,比如: 用于服务器性能监视的PerfMon Metrics Collector 用于建立压 ...

  7. 解决gradle project refresh failed: protocol family unavailable问题的几种方法

    Android Studio从版本1.5更新到2.1之后,打开Android Studio一直提示: gradle project refresh failed: protocol family un ...

  8. JS——滚动条

    1.核心思想与之前的拖拽盒子是一样的 2.完全将鼠标在盒子中的坐标给滚动条是错的,因为这样会使滚动条顶部立刻瞬间移动到鼠标位置 3.必须在鼠标按下事件时记住鼠标在滚动条内部的坐标,再将鼠标在盒子中的坐 ...

  9. js输出非字符串,非null值

    console.log(!"");//非空(true) console.log(!);//非0(true) console.log(!" ");//非空格(fa ...

  10. ipv4的设置

    有段时间自己的网总是连不上,别人的都可以,因为公司又wifi,就将就着用wifi了,没有去查看原因,后来由于公司1网段大部分ip号被占用,系统要接入32路主机测试,每个主机都要分配ip,只好开辟2网段 ...