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. 【iOS】Updating local specs repositories

    使用 Pods 时遇到这个问题,原因是被墙了……需换成下面命令: pod install --verbose --no-repo-update

  2. linux下pip的安装

    ---恢复内容开始--- 1 输入apt-cache search wxpython 如果有返回信息 则输入 sudo apt-get install python-tools 2 否则 1.添加软件 ...

  3. linux字符设备驱动中内核如何调用驱动入口函数 一点记录

    /* 内核如何调用驱动入口函数 ? *//* 答: 使用module_init()函数,module_init()函数定义一个结构体,这个结构体里面有一个函数指针,指向first_drv_init() ...

  4. 【Python-django后端开发】logging日制配置详解!!!

    官方文档请查看:https://docs.djangoproject.com/en/1.11/topics/logging/ 1. 配置工程日志,在setting.py里,如下 LOGGING = { ...

  5. RabbitMQ(四):使用Docker构建RabbitMQ高可用负载均衡集群

    本文使用Docker搭建RabbitMQ集群,然后使用HAProxy做负载均衡,最后使用KeepAlived实现集群高可用,从而搭建起来一个完成了RabbitMQ高可用负载均衡集群.受限于自身条件,本 ...

  6. Iphone使用过程中遇到的问题

    Q1:同一个Apple ID不同设备之间的通话记录保持同步 解决方法: Step1:"设置"--"电话"--"在其他设备上通话"--选择关闭 ...

  7. SpringMVC学习笔记之---数据绑定

    SpringMVC数据绑定 一.基础配置 (1)pom.xml <dependencies> <dependency> <groupId>junit</gro ...

  8. 理解MySQL(一)--MySQL介绍

    一.Mysql逻辑架构: 1. 第一层:服务器层的服务,连接\线程处理. 2. 第二层:查询执行引擎,MySQL的核心服务功能,包括查询解析.分析.优化和缓存,所有跨存储引擎的功能都在这一层实现. 3 ...

  9. Java学习|HTTP请求头

    https://www.cnblogs.com/honghong87/articles/6941436.html 常见http请求报文头属性      Accept:告诉服务端,客户端接受什么类型的响 ...

  10. Spring入门(九):运行时值注入

    Spring提供了2种方式在运行时注入值: 属性占位符(Property placeholder) Spring表达式语言(SpEL) 1. 属性占位符 1.1 注入外部的值 1.1.1 使用Envi ...