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 ...
随机推荐
- CentOS6下基于Nginx搭建mp4/flv流媒体服务器
CentOS6下基于Nginx搭建mp4/flv流媒体服务器(可随意拖动)并支持RTMP/HLS协议(含转码工具) 1.先添加几个RPM下载源 1.1)安装RPMforge的CentOS6源 [roo ...
- firfox浏览器常用快捷键
Ctrl + 数字键来打开第N个标签页这种还要先数完再到键盘上找数字Ctrl + Page Up = 激活左边一个标签页Ctrl + Page Down = 激活右边一个标签页Ctrl + Tab = ...
- programming review (c++): (2)binary tree, BFS, DFS, recursive, non-recursive
1.二叉树定义 // Definition for a binary tree node. struct TreeNode { int val; TreeNode *left; TreeNode *r ...
- ubuntu 中文显示乱码问题 (转)
添加中文字符编码: $sudo vim /var/lib/locales/supported.d/local #添加下面的中文字符集 zh_CN.GBK GBK zh_CN.GB2312 GB2312 ...
- Android_YouthArea之ApeendTextView
这次给我自己的项目打个广告:http://sj.qq.com/myapp/detail.htm?apkName=com.youthcommunity 这款APP 不同于SoHOT是积极的,是年轻人的信 ...
- java 多参实现
package com.northeasttycoon.monitor.service; import static java.lang.System.out; /** * Created by no ...
- 微信小程序TabBar的使用
一.TabBar使用步骤 1.创建所需要的界面和所需要的图片: 2.配置文件: 我们找到项目根目录中的配置文件 app.json 加入如下配置信息 "tabBar": { &quo ...
- 之前博客中的代码都放到github上
之前一直把代码托管在taocode上,现在已经不能用了,所以把代码整理了一下,统一都放在gibhub上了. LALR(1)语法分析生成器:https://github.com/kiven-li/xby ...
- android菜鸟学习笔记12----Android控件(一) 几个常用的简单控件
主要参考<第一行代码> 1.TextView: 功能与传统的桌面应用开发中的Label控件相似,用于显示文本信息 如: <TextView android:layout_width= ...
- 10分钟看懂, Java NIO 底层原理
目录 写在前面 1.1. Java IO读写原理 1.1.1. 内核缓冲与进程缓冲区 1.1.2. java IO读写的底层流程 1.2. 四种主要的IO模型 1.3. 同步阻塞IO(Blocking ...