hdu 2222 Keywords Search——AC自动机
题目: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自动机的更多相关文章
- hdu 2222 Keywords Search ac自动机入门
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:有N(N <= 10000)个长度不超过50的模式串和一个长度不超过1e6的文本串. ...
- HDU 2222 Keywords Search(AC自动机模板题)
学习AC自动机请戳这里:大神blog........ 自动机的模板: #include <iostream> #include <algorithm> #include < ...
- HDU 2222 Keywords Search (AC自动机)
题意:就是求目标串中出现了几个模式串. 思路:用int型的end数组记录出现,AC自动机即可. #include<iostream> #include<cstdio> #inc ...
- hdu 2222 Keywords Search ac自动机模板
题目链接 先整理一发ac自动机模板.. #include <iostream> #include <vector> #include <cstdio> #inclu ...
- HDU 2222 Keywords Search (AC自动机)(模板题)
<题目链接> 题目大意: 给你一些单词,和一个字符串,问你这个字符串中含有多少个上面的单词. 解题分析: 这是多模匹配问题,如果用KMP的话,对每一个单词,都跑一遍KMP,那么当单词数量非 ...
- hdu 2222 Keywords Search - Aho-Corasick自动机
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submissio ...
- hdoj 2222 Keywords Search(AC自动机)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 思路分析:该问题为多模式匹配问题,使用AC自动机解决:需要注意的问题是如何统计该待查询的字符串包 ...
- hdu 2222 Keywords Search ac自己主动机
点击打开链接题目链接 Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Ja ...
- HDU 2222 Keywords Search AC自己主动机入门题
单词统计的题目,给出一些单词,统计有多少单词在一个文本中出现,最经典的入门题了. AC自己主动机的基础: 1 Trie. 以这个数据结构为基础的,只是添加一个fail指针和构造fail的函数 2 KM ...
随机推荐
- Oracle:复合触发器
----- CF_DEPTUCORGANIZATION INSERT UPDATE DELETE 触发器CREATE OR REPLACE TRIGGER tr_del_CF_DEPTUCORGA ...
- 设置mysql隔离级别
1.查看当前会话隔离级别 select @@tx_isolation; 2.查看系统当前隔离级别 select @@global.tx_isolation; 3.设置当前会话隔离级别 set sess ...
- poj1206(dp)
题目链接:http://poj.org/problem?id=1260 Pearls Time Limit: 1000MS Memory Limit: 10000K Total Submissio ...
- 搭建私有Nuget仓库
使用Nexus搭建私有Nuget仓库 https://www.cnblogs.com/Erik_Xu/p/9211471.html 前言 Nuget是ASP .NET Gallery的一员,是免费.开 ...
- zabbix-agent active 配置自动探测
1. zabbix-agent 被动模式配置文件: PidFile=/var/run/zabbix/zabbix_agentd.pid LogFile=/var/log/zabbix/zabbix_a ...
- 模式识别开发之项目---基于opencv的手势识别
我使用OpenCV2.4.4的windows版本+Qt4.8.3+VS2010的编译器做了一个手势识别的小程序. 本程序主要使到了Opencv的特征训练库和最基本的图像处理的知识,包括肤色检测等等. ...
- Oracle exp使用正則表達式导出部分表
假设数据库中有许多张表,而我们又仅仅想导出须要的那几张能够使用例如以下命令 --仅仅导出test1.test2表 exp myname/mypassword@orcl file = d:\my.dmp ...
- 在java项目中使用protobuf
1 通用方式 第一步,定义数据结构 第二步,使用protoc.exe生成java代码 第三步,序列化 第四步,反序列化 2 grpc方式 grpc官方推荐的方式,通过maven插件来生成java代码. ...
- 如何在windows上创建文件名以“.”开头的文件
比如要创建.env文件,正常会提示必须输入文件名才能创建的,但是可以在后面再加一个点就能创建了,.env.这样就可以了
- web前端开发-Ajax(1)
1.简单简绍Ajax的功能 Ajax是处于前端和后端之间的这么一个东西,他可以拿到你前端form的内容,并且在你触发Ajax的时候,先将某些数据发送到服务器端,等接受到服务器 返回的数据时,执行某个函 ...