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. httpclient信任所有证书解决SSLException:Unrecognized SSL message,plaintext connection

    在使用 HttpClient 工具调用第三方 Http 接口时报错 javax.net.ssl.SSLException:Unrecognized SSL message,plaintext conn ...

  2. 【iOS】NSNotification 常用方法

    NSNotification 常用的几个方法,代码如下: // 发送通知 [[NSNotificationCenter defaultCenter] postNotificationName:@&qu ...

  3. win10应用商店卸载后重装教程

    方法一 先进这个链接   http://go.microsoft.com/fwlink/?LinkId=619547  下载一个记事本文件,并且把它保存到你的“下载” 里面. 管理员身份打开Power ...

  4. 自定义SWT控件七之自定义Shell(可伸缩窗口)

    7.可伸缩窗口 该自定义窗口可以通过鼠标随意更改窗口大小 package com.hikvision.encapsulate.view.control.shell; import org.eclips ...

  5. mysql 查询结果显示行号

    mysql 查询时,不像oracle那样,可以直接用 rownum 显示结果行号. 可以用定义用户变量来实现 set @myrnum = 0; select (@myrnum := @myrnum + ...

  6. JAVA基础知识(八)值传递与引用传递

    值传递:(形式参数类型是基本数据类型):方法调用时,实际参数把它的值传递给对应的形式参数,形式参数只是用实际参数的值初始化自己的存储单元内容,是两个不同的存储单元,所以方法执行中形式参数值的改变不影响 ...

  7. GIS历史概述与WebGis应用开发技术浅解

    声明:本篇在李晓晖的<杂谈WebGIS>,补充更多的资料说明.基于地图二次开发一直断断续续在做,这里算是补充一下基本功把.其实对于前端,WebGis开发都是api,抄demo,改.GIS深 ...

  8. 【Kubernetes 系列三】Kubernetes 学习文档推荐

    标题 地址 备注 Kubernetes 官方文档 https://kubernetes.io/docs 英文文档,全面 Kubernetes Handbook ttps://jimmysong.io/ ...

  9. 变量Variable

    变量Variable 内存 #conding:utf-8 a = 1 #conding:utf-8 a = 1 b = a #conding:utf-8 a = 1 b = a a = 2 命名规则 ...

  10. 5.MySQL数据库操作步骤

    第一步:登录到MySQL服务器 第二步:选择当前要操作的数据库 第三步:设置请求和返回数据的字符集 第四步:执行SQL语句 l 增加记录:INSERT INTO news(title,content) ...