题目:http://acm.hdu.edu.cn/showproblem.php?pid=2222

第一道AC自动机!

T了无数边后终于知道原来它是把若干询问串建一个自动机,把模式串放在上面跑;而且只走模式串的前缀,用 fail 指针来精准遍历每个前缀的每个后缀,就能行了。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=1e6+,K=;
int T,n,c[N][K],fail[N],ed[N],len,ans,tot;
int q[N],he,tl;
char ch[N];
void init()
{
tot=;ans=he=tl=;
memset(c,,sizeof c);memset(fail,,sizeof fail);
memset(ed,,sizeof ed);
}
void insert()
{
int nw=;
for(int i=,d;i<=len;i++)
{
d=ch[i]-'a';
if(!c[nw][d])
c[nw][d]=++tot;
nw=c[nw][d];
}
ed[nw]++;
}
void gtfl()
{
for(int i=;i<;i++)
if(c[][i])
{
q[++tl]=c[][i];
fail[c[][i]]=;
}
while(he<tl)//<
{
int k=q[++he];
for(int i=;i<;i++)
if(c[k][i])
{
q[++tl]=c[k][i];
int nw=fail[k];
while(!c[nw][i]&&nw!=)
nw=fail[nw];
if(!c[nw][i]) fail[c[k][i]]=;
else fail[c[k][i]]=c[nw][i];
}
}
}
void solve()
{
int nw=;
for(int i=,d,cr;i<=len;i++)//只走一遍前缀
{
d=ch[i]-'a';
while(!c[nw][d]&&nw!=) nw=fail[nw];
nw=c[nw][d]; if(!nw) nw=; cr=nw;
while(cr!=&&ed[cr])//遍历该前缀的每个后缀
{
ans+=ed[cr];ed[cr]=;cr=fail[cr];
}
}
}
int main()
{
scanf("%d",&T);
while(T--)
{
init();
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%s",ch+); len=strlen(ch+);
insert();
}
gtfl();
scanf("%s",ch+); len=strlen(ch+);
solve();
printf("%d\n",ans);
}
return ;
}

hdu 2222 Keywords Search——AC自动机的更多相关文章

  1. hdu 2222 Keywords Search ac自动机入门

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:有N(N <= 10000)个长度不超过50的模式串和一个长度不超过1e6的文本串. ...

  2. HDU 2222 Keywords Search(AC自动机模板题)

    学习AC自动机请戳这里:大神blog........ 自动机的模板: #include <iostream> #include <algorithm> #include < ...

  3. HDU 2222 Keywords Search (AC自动机)

    题意:就是求目标串中出现了几个模式串. 思路:用int型的end数组记录出现,AC自动机即可. #include<iostream> #include<cstdio> #inc ...

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

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

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

    <题目链接> 题目大意: 给你一些单词,和一个字符串,问你这个字符串中含有多少个上面的单词. 解题分析: 这是多模匹配问题,如果用KMP的话,对每一个单词,都跑一遍KMP,那么当单词数量非 ...

  6. hdu 2222 Keywords Search - Aho-Corasick自动机

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submissio ...

  7. hdoj 2222 Keywords Search(AC自动机)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 思路分析:该问题为多模式匹配问题,使用AC自动机解决:需要注意的问题是如何统计该待查询的字符串包 ...

  8. hdu 2222 Keywords Search ac自己主动机

    点击打开链接题目链接 Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Ja ...

  9. HDU 2222 Keywords Search AC自己主动机入门题

    单词统计的题目,给出一些单词,统计有多少单词在一个文本中出现,最经典的入门题了. AC自己主动机的基础: 1 Trie. 以这个数据结构为基础的,只是添加一个fail指针和构造fail的函数 2 KM ...

随机推荐

  1. 个人笔记-CSS

    http://localhost:1081/sdfsdfs/config-browser/actionNames.action 超出容器文字隐藏 .hiddenoverflowtext { width ...

  2. Zabbix-20160817-高危SQL注入漏洞

    漏洞概述: zabbix是一个开源的企业级性能监控解决方案.近日,zabbix的jsrpc的profileIdx2参数存在insert方式的SQL注入漏洞,攻击者无需授权登陆即可登陆zabbix管理系 ...

  3. HDFS源码分析之编辑日志编辑相关双缓冲区EditsDoubleBuffer

    EditsDoubleBuffer是为edits准备的双缓冲区.新的编辑被写入第一个缓冲区,同时第二个缓冲区可以被flush.为edits准备的双缓冲区.新的编辑被写入第一个缓冲区,同时第二个缓冲区可 ...

  4. python 基础 7.1 datetime 获得时间

    一 datatime 的使用   object         timedeta         tzinfo         time         data                dat ...

  5. java的Access restriction错误

    问 :import sun.management.ManagementFactory,我在rt包下已经找到sun.management.ManagementFactory,但就是有错,请问怎么回事. ...

  6. 使用python实现二分法查找

    最近开始学习mit的python课程,其中手工实现的一个关于二分法查找的练习代码个人感觉比较有参考价值,贴上来分享交流一下. 主要功能是在1-100中自己猜测一个数值,随后系统产生数值看是否符合猜测, ...

  7. python cookbook第三版学习笔记四:文本以及字符串令牌解析

    文本处理: 假设你存在一个目录,下面存在各种形式的文件,有txt,csv等等.如果你只想找到其中一种或多种格式的文件并打开该如何办呢.首先肯定是要找到满足条件的文件,然后进行路径合并在一一打开. pa ...

  8. python cookbook第三版学习笔记八:解析码流

    Binascii模块 通过名字很容易知道这个模块的作用,binary, ascii.两个字母的合成,也就是二进制和ascii的转换模块 下面先介绍下字母的ascii码 a-z的ascii是从97-12 ...

  9. js 事件委托 bug 修复

    下面是html 内容: <ul id="oul"> <li>1</li> <li>2</li> <li>3& ...

  10. 应用提交 App Store 上架被拒绝

    审核的参考资料 https://developer.apple.com/icloud/documentation/data-storage/index.html https://developer.a ...