http://blog.csdn.net/niushuai666/article/details/7002823

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
struct node{
int count;
node *next[26],*fail;
void Node(){
count = 0;fail = NULL;
memset(next,NULL,sizeof(next));
}
}arr[300005],*que[500005];
char str[1000006];
int cnt = 0;
void insert(char *str){
node *p = &arr[0];
while( *str ){
int k = *str++ - 'a';
if( !p->next[k] ){
arr[++cnt].Node();
p->next[k] = &arr[cnt];
}
p = p->next[k];
}
p->count++;
} void build_AC(){
node *root = &arr[0],*tmp,*p;
int head = 0,tail = 0;
for(int i = 0; i < 26; i++){
if(!root->next[i])continue;
root->next[i]->fail = root;
que[head++] = root->next[i];
}
while( head != tail){
tmp = que[tail++];
for(int i = 0; i < 26 ; i++){
if( tmp->next[i] ){
p = tmp->fail;
while( p ){
if( p->next[i] ){
tmp->next[i]->fail = p->next[i];
break;
}
p = p->fail;
}
if(!p)tmp->next[i]->fail = root;
que[head++] = tmp->next[i];
}
}
}
}
int query(char *str){
node *root = &arr[0],*p = &arr[0];
int ans= 0;
while( *str ){
int k = *str++ - 'a';
while( !p->next[k] && p != root) p = p->fail;
p = p->next[k];
p = p ? p : root;
node *tmp = p;
while( tmp != root && tmp->count != -1){
ans += tmp->count;
tmp->count = -1;
tmp = tmp->fail;
}
}
return ans;
}
int main(){
// freopen("in.txt","r",stdin);
int t,n;
char ch[60];
scanf("%d",&t);
while( t-- ){
scanf("%d",&n);
arr[cnt = 0].Node();
while( n-- ){
scanf("%s",ch);
insert(ch);
}
build_AC();
scanf("%s",str);
printf("%d\n",query(str));
}
return 0;
}

  

HDU2222Keywords Search AC_自动机的更多相关文章

  1. 2014 网选 5007 Post Robot(暴力或者AC_自动机(有点小题大作了))

    //暴力,从每一行的开始处开始寻找要查询的字符 #include<iostream> #include<cstdio> #include<cstring> #inc ...

  2. spoj 7258 Lexicographical Substring Search (后缀自动机)

    spoj 7258 Lexicographical Substring Search (后缀自动机) 题意:给出一个字符串,长度为90000.询问q次,每次回答一个k,求字典序第k小的子串. 解题思路 ...

  3. 【hdu2222-Keywords Search】AC自动机基础裸题

    http://acm.hust.edu.cn/vjudge/problem/16403 题意:给定n个单词,一个字符串,问字符串中出现了多少个单词.(若单词her,he,字符串aher中出现了两个单词 ...

  4. 【HDU2222】Keywords Search AC自动机

    [HDU2222]Keywords Search Problem Description In the modern time, Search engine came into the life of ...

  5. hdu2222 Keywords Search ac自动机

    地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=2222 题目: Keywords Search Time Limit: 2000/1000 MS ...

  6. HDU2222 Keywords Search [AC自动机模板]

    Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others ...

  7. hdu_2222_Keywords Search(AC自动机板子)

    题目连接:hdu_2222_Keywords Search 存个自己写的AC自动机 #include<cstdio> #include<cstring> #define F(i ...

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

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

  9. Keywords Search(AC自动机模板)

    Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others ...

随机推荐

  1. 【Java】判断字符串是否含字母

    用正则表达式,示例代码如下: String str = "123abc"; Pattern.compile("(?i)[a-z]]").matcher(str) ...

  2. 关于ajax异步请求的一个细节问题

    首先描述一下问题场景:我们正在做一个汽车出租项目,使用maven+ssm+easyui来完成,这个问题是在做汽车办理出租业务的时候出现的. 问题描述:在使用ajax发送异步请求时,遇到一个问题,就是在 ...

  3. vmware15pro安装ubuntu18.10时出现显示不全问题

    如果这个时候用网上的ALT+左键拖拽根本没有效果 所以这里提供另外一种方式 就是正常安装的时候发现分区部分显示不全 此时点击右上角的橙色小×:询问是否退出 我们点击退出:之后就会来到试用界面 到了这里 ...

  4. 一文了解:Redis基础类型

    Redis基础类型 Redis特点 开源的,BSD许可高级的key-value存储系统 可以用来存储字符串,哈希结构,链表,集合 安装 windows:https://github.com/micro ...

  5. Linux基础用户管理

    一.用户管理 (一).用户和组的基本概念 Users and groups:. Every process (running program) on the system runs as a part ...

  6. Unity经典游戏教程之:是男人就下100层

    版权声明: 本文原创发布于博客园"优梦创客"的博客空间(网址:http://www.cnblogs.com/raymondking123/)以及微信公众号"优梦创客&qu ...

  7. 【作品】超实用C++分数类

    引言 我们说,编程语言的精髓在于封装,而面向对象语言完胜面向过程语言的原因就是具有更好的可封装性,而C++就是这样的一种多范型语言,常用而复杂的工作完全不必在每一份源文件中重敲,就好像我们不需要自己手 ...

  8. 想成为顶尖 Java 程序员?请先过了下面这些技术问题。

    一.数据结构与算法基础 说一下几种常见的排序算法和分别的复杂度. 用Java写一个冒泡排序算法 描述一下链式存储结构. 如何遍历一棵二叉树? 倒排一个LinkedList. 用Java写一个递归遍历目 ...

  9. Flink 源码解析 —— 如何获取 ExecutionGraph ?

    https://t.zsxq.com/UnA2jIi 博客 1.Flink 从0到1学习 -- Apache Flink 介绍 2.Flink 从0到1学习 -- Mac 上搭建 Flink 1.6. ...

  10. 如何实现css渐变圆角边框

    最近设计师的风格发生突变,一句话概括就是,能用渐变的地方绝对不用纯色.这不,就整出了一个渐变圆角边框.这渐变好做,圆角好做,渐变圆角也没问题,可是在加个边框还是有点坑的.没办法,看看怎么实现吧 bor ...