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. poj3539 Elevator——同余类bfs

    题目:http://poj.org/problem?id=3539 题目大意是给定 a, b, c,求 1~h 内有多少个数可以被 a, b, c 通过加减法组成: 这是今天刚讲的神奇的——同余类 b ...

  2. 使用psutil模块获取电脑运行信息

    psutil是python的一个用于获取cpu信息的模块,非常好使,以下附上官方的一些example: CPU-> Examples >>> import psutil > ...

  3. Complicated Expressions(表达式转换)

    http://poj.org/problem?id=1400 题意:给出一个表达式可能含有多余的括号,去掉多余的括号,输出它的最简形式. 思路:先将表达式转化成后缀式,因为后缀式不含括号,然后再转化成 ...

  4. P3199 [HNOI2009]最小圈

    传送门 据rqy说有这么一个结论\[ans=\min_{v \in V,F_n(v)\neq \infty} \max_{0 \leq k \leq n - 1} \left[\frac{F_n(v) ...

  5. [Swift通天遁地]七、数据与安全-(15)使用单元测试进行代码的性能分析

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  6. Redis基本属性的使用-详细

    Redis 数据结构简介 Redis 可以存储键与5种不同数据结构类型之间的映射,这5种数据结构类型分别为String(字符串).List(列表).Set(集合).Hash(散列)和 Zset(有序集 ...

  7. 题解报告:hdu 2647 Reward(拓扑排序)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2647 Problem Description Dandelion's uncle is a boss ...

  8. HTML TabIndex属性

    TabIndex作用: tabindex:全局属性.指示其元素是否可以聚焦(获得焦点),以及它是否/在何处参与顺序键盘导航(因通常使用tab键操作,顾因此得名). 当使用tab键在网页控件中进行导航时 ...

  9. python gdal 修改shp文件的属性值

    driver = ogr.GetDriverByName('ESRI Shapefile')datasource = driver.Open(shpFileName, 1)layer = dataso ...

  10. Python3 每次处理一个字符

    """ Python3.4[文本]之每次处理一个字符 """ test_str = "my name is bixiaopeng& ...