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连接MySQL8.0以上版本数据库方式

    MySQL 8.0 开始数据库相比常用的 5.X 版本发生了比较大的变化,我们在连接数据库的过程中许多地方也要发生一些变化. 总结一下,想要利用 mysql-connector-java 与 MySQ ...

  2. 【iOS】iOS CocoaPods 整理

    github 上下载 Demo 时第一次遇到这个情况,当时有些不知所措,也没怎么在意,后来项目调整结构时正式见到了这个,并且自己去了解学习了. CocoaPods安装和使用教程 这篇文章写得很好!ma ...

  3. 二进制文件安装k8s所需要的证书服务

    利用二进制文件安装etcd所需要的证书服务 CFSSL是CloudFlare开源的一款PKI/TLS工具. CFSSL 包含一个命令行工具 和一个用于 签名,验证并且捆绑TLS证书的 HTTP API ...

  4. 使用Kubeadm创建k8s集群之节点部署(三十一)

    前言 本篇部署教程将讲述k8s集群的节点(master和工作节点)部署,请先按照上一篇教程完成节点的准备.本篇教程中的操作全部使用脚本完成,并且对于某些情况(比如镜像拉取问题)还提供了多种解决方案.不 ...

  5. Extjs4 combobox autoLoad: true 时,加载两次

    问题是这样的,combobox 远程加载数据时,当我们把 store 设置为  autoLoad: tue, 时,这样页面加载时,store 会load 一次,但是我们在第一次点击 下来框时,他还会 ...

  6. 【Java例题】6.2 日期类的使用

    2.日期类的使用.显示今天的年月日.时分秒和毫秒数.显示今天是星期几.是今年内的第几天.显示本月共几天,今年是不是闰年.显示两个日期的差,包括年月日.时分秒和毫秒差值. package chapter ...

  7. requestAnimationFrame 兼容方案

    [toc] 编写涉及:css, html, js 在线演示codepen html代码 <div class="roll-box"> <div class=&qu ...

  8. Javaweb表格加载---DataTable

    Datatables是一款jquery表格插件.它是一个高度灵活的工具,可以将任何HTML表格添加高级的交互功能. 使用 jQuery Datatable 构造数据列表,并且增加或者隐藏相应的列,已达 ...

  9. linux装OpenOffice后传---中文乱码的解决

    上一篇的博客已经详细的介绍了linux系统上如何安装OpenOffice,安装之后使用发现转换的pdf出现中文乱码.后来发现是linux上没有中文对应的那个字体. 字体准备 在windows上的位置 ...

  10. 深度搜索(dfs)+典型例题(八皇后)

    深度优先搜索简称深搜,从起点出发,走过的点要做标记,发现有没走过的点,就随意挑一个往前走,走不了就回退,此种路径搜索策略就称为“深度优先搜索”,简称“深搜”. 如上面的图所示:加入我们要找一个从V0到 ...