题意:就是求目标串中出现了几个模式串。

思路:用int型的end数组记录出现,AC自动机即可。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#include<queue>
#define maxn 500005
int T,n;
char s[];
struct Trie{
int root,L;
int next[maxn][],fail[maxn],end[maxn];
int newnode(){
for (int i=;i<;i++)
next[L][i]=-;
end[L++]=;
return L-;
}
void clear(){
L=;root=newnode();
}
void insert(char s[]){
int now=root,len=strlen(s);
for (int i=;i<len;i++){
if (next[now][s[i]-'a']==-) next[now][s[i]-'a']=newnode();
now=next[now][s[i]-'a'];
}
end[now]++;
}
void build(){
std::queue<int>Q;
int now=root;
for (int i=;i<;i++)
if (next[now][i]==-) next[now][i]=root;
else
fail[next[now][i]]=root,Q.push(next[now][i]);
while (!Q.empty()){
int now=Q.front();
Q.pop();
for (int i=;i<;i++){
if (next[now][i]==-)
next[now][i]=next[fail[now]][i];
else{
fail[next[now][i]]=next[fail[now]][i];
Q.push(next[now][i]);
}
}
}
}
int query(char s[]){
int now=root,len=strlen(s),res=;
for (int i=;i<len;i++){
now=next[now][s[i]-'a'];
int tmp=now;
while (tmp!=root){
res+=end[tmp];
end[tmp]=;
tmp=fail[tmp];
}
}
return res;
}
}ac;
int main(){
scanf("%d",&T);
while (T--){
scanf("%d",&n);
ac.clear();
for (int i=;i<n;i++){
scanf("%s",s);
ac.insert(s);
}
ac.build();
scanf("%s",s);
printf("%d\n",ac.query(s));
}
}

HDU 2222 Keywords Search (AC自动机)的更多相关文章

  1. hdu 2222 Keywords Search——AC自动机

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=2222 第一道AC自动机! T了无数边后终于知道原来它是把若干询问串建一个自动机,把模式串放在上面跑:而且只 ...

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

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

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

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

  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. QT类的继承结构

    QT类的继承结构 QT的类 core 数据集合 QString 几何类 QPoint QSize QRectangle 系统类 QColor QFont QImage QIcon QCursor QB ...

  2. Linux企业级项目实践之网络爬虫(15)——区分文本文件和二进制文件

    HTTP协议支持文本和二进制文件传输.最常见的html格式的页面即文本,图片.音乐等为二进制文件.我们要对这两类文件加以区分并分别处理. static char * BIN_SUFFIXES = &q ...

  3. 【转】四步完成win7 ubuntu双系统安装(硬盘,无需光驱)

    原文网址:http://ifeiyang.cn/archives/1835.html 适用环境: 理论上win7.vista系统32位或64位均可.ubuntu适用与10.X版本,且ubuntu-10 ...

  4. hdu4521-小明系列问题——小明序列(线段树区间求最值)

    题意:求最长上升序列的长度(LIS),但是要求相邻的两个数距离至少为d,数据范围较大,普通dp肯定TLE.线段树搞之就可以了,或者优化后的nlogn的dp. 代码为  线段树解法. #include ...

  5. 【POJ1338】Ugly Numbers(暴力打表)

    打表大军是一股邪恶势力→_→ #include <iostream> #include <cstring> #include <cstdlib> #include ...

  6. mysql 中文乱码的解决方法

    添加或修改my.ini 配置文件,设置编码字符为utf8 ,默认为latin1,见红色字体 [mysql]# 设置mysql客户端默认字符集default-character-set=utf8 [my ...

  7. python网络请求简洁之道--python requests简介

    #requests中文文档:http://cn.python-requests.org/en/latest/#学习出处:http://mp.weixin.qq.com/s?__biz=MjM5NzU0 ...

  8. Android ActionBar完全解析,使用官方推荐的最佳导航栏(下) .

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/25466665 本篇文章主要内容来自于Android Doc,我翻译之后又做了些加工 ...

  9. Citrix 服务器虚拟化之十 Xenserver高可用性HA

    Citrix 服务器虚拟化之十 Xenserver高可用性HA HA是一套全自动功能设计,规划.它可以安全地恢复出现问题的XenServe 主机.例如物理破坏网络或主机的硬件故障,HA可确保无需任何人 ...

  10. java基础之代理

    代理的定义,代理的应用,代理的特性