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

思路:用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. 疯狂delphi - 朱建强 (一些小例子很实用,也是我所关心的几个问题)

    疯狂delphi - 朱建强 (一些小例子很实用,也是我所关心的几个问题) Android实例-获取安卓手机WIFI信息(XE8+小米2)http://www.cnblogs.com/FKdelphi ...

  2. 《iPhone高级编程—使用Mono Touch和.NET/C#》

    第1章 C#开发人员基于MonoTouch进行iPhone开发概述 1 1.1 产品对比 2 1.1.1 .NET Framework 2 1.1.2 Mono 2 1.1.3 MonoTouch 3 ...

  3. Linux企业级项目实践之网络爬虫(3)——设计自己的网络爬虫

    网络抓取系统分为核心和扩展组件两部分.核心部分是一个精简的.模块化的爬虫实现,而扩展部分则包括一些便利的.实用性的功能.目标是尽量的模块化,并体现爬虫的功能特点.这部分提供简单.灵活的API,在基本不 ...

  4. 【转】64位win7硬盘安装64位ubuntu 13.04

    原文网址:http://www.cnblogs.com/jiangz/p/3751617.html 最近本来是准备通过升级的方式把ubuntu从12.04升级到12.10再升级到13.04的,但是升级 ...

  5. 40 个超棒的免费 Bootstrap HTML5 网站模板

    Bootstrap 是快速开发Web应用程序的前端工具包.它是一个CSS和HTML的集合,它使用了最新的浏览器技术,给你的Web开发提供了时尚的版式,表单,buttons,表格,网格系统等等. 目前 ...

  6. PHP中文汉字验证码

    hb.ttf换成随便你自己下载的ttf Header("Content-type: image/PNG"); $str="的一是在了不和有大这主中人上为们地个用工时要动国 ...

  7. 关于hibernate中对象的三种状态分析

    一,首先hibernate中对象的状态有三种:瞬态.游离态和持久态,三种状态转化的方法都是通过session来调用,瞬态到持久态的方法有save().saveOrUpdate().get().load ...

  8. iOS UIView显示颜色渐变

    -(void)colorWear:(UIView *)view{ CAGradientLayer *gradient = [CAGradientLayer layer]; gradient.frame ...

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

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

  10. iphone开发之适配iphone5

    iphone5出来了,从不用适配的我们也要像android一样适配不同分辨率的屏幕了.   公司产品新版本需要适配iphone5,经过一番折腾算是搞定了.下面分享给大家:   iphone5的屏幕分辨 ...