题目: http://acm.hdu.edu.cn/showproblem.php?pid=2222

AC自动机模板题

我现在对AC自动机的理解还一般,就贴一下我参考学习的两篇博客的链接:

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

http://www.cppblog.com/menjitianya/archive/2014/07/10/207604.html

 #include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
char key[];
char des[];
struct node{
node *fail;
node *next[];
int cnt;
node(){
fail = NULL;
cnt = ;
for(int i = ;i<;i++)
next[i] = NULL;
}
};
node *root;
void insert(char *str){
node *head = root;
int len = strlen(str);
for(int i = ;i<len;i++){
int temp = str[i]-'a';
if(head->next[temp] == NULL)
head->next[temp] = new node();
head = head->next[temp];
}
head->cnt++;
}
void build(){
queue<node *>q;
q.push(root);
while(!q.empty()){
node *head = q.front();
q.pop();
for(int i = ;i<;i++){
if(head->next[i] != NULL){
if(head == root){
head->next[i]->fail = root;
}else{
node *temp = head->fail;
while(temp != NULL){
if(temp->next[i] != NULL){
head->next[i]->fail = temp->next[i];
break;
}
temp = temp->fail;
}
if(temp == NULL)
head->next[i]->fail = root;
}
q.push(head->next[i]);
}
}
}
}
int query(){
int len = strlen(des),ans = ;;
node *head = root;
for(int i = ;i<len;i++){
int index = des[i]-'a';
while(head->next[index] == NULL && head != root)
head = head->fail;
head = head->next[index];
if(head == NULL)
head = root;
node *temp = head;
while(temp!=root && temp->cnt!=-){
ans += temp->cnt;
temp->cnt = -;
temp = temp->fail;
}
}
return ans;
}
int main(){
int t;
scanf("%d",&t);
while(t--){
root = new node();
int n;
scanf("%d",&n);
for(int i = ;i<n;i++){
scanf(" %s",key);
insert(key);
}
build();
scanf(" %s",des);
printf("%d\n",query());
}
return ;
}

HDU 2222 AC自动机模板题的更多相关文章

  1. HDU 3065 (AC自动机模板题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3065 题目大意:多个模式串,范围是大写字母.匹配串的字符范围是(0~127).问匹配串中含有哪几种模 ...

  2. HDU 2896 (AC自动机模板题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2896 题目大意:多个模式串.多个匹配串.其中串的字符范围是(0~127).问匹配串中含有哪几个模式串 ...

  3. hdu 2222(AC自动机模版题)

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

  4. HDU 2222 AC自动机 裸题

    题意: 问母串中出现多少个模式串 注意ac自动机的节点总数 #include <stdio.h> #include <string.h> #include <queue& ...

  5. HDU 2222 AC自动机模版题

    所学的AC自动机都源于斌哥和昀神的想法. 题意:求目标串中出现了几个模式串. 使用一个int型的end数组记录,查询一次. #include <cstdio> #include <c ...

  6. HDU 2222 & ac自动机模板

    题意: 求n个模板串在匹配串中出现了几个. SOL: 反正就是模板啦...似乎比KMP都简单----这么说似乎有点不道德...毕竟先看的KMP而他们并没有什么不同... 貌似自己的理解和他们画的图还是 ...

  7. HDU 2222 AC自动机(模版题)

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

  8. Keywords Search HDU - 2222 AC自动机板子题

    In the modern time, Search engine came into the life of everybody like Google, Baidu, etc. Wiskey al ...

  9. HDU 2222(AC自动机模板题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2222 题目大意:多个模式串.问匹配串中含有多少个模式串.注意模式串有重复,所以要累计重复结果. 解题 ...

随机推荐

  1. IOS基础之 (设计模式)

    一 工厂方法 工厂方法方便我们快速创建类的实例的方法.通过工厂方法,可以让调用过程更加清晰. Person.h #import <Foundation/Foundation.h> @int ...

  2. [POJ2773]:Happy 2006

    传送门 同样是欧拉函数的基本应用. $\phi (N)$表示$[1,N]$中,$gcd(i,N)==1$的数的个数,同理,其也能表示$[K \times N+1,(K+1) \times N]$中$g ...

  3. 在linux下如何将文件夹打包

    tar tar命令可以用来压缩打包单文件.多个文件.单个目录.多个目录. 常用格式: 单个文件压缩打包 tar czvf my.tar file1 多个文件压缩打包 tar czvf my.tar f ...

  4. eclipse配置gradle

    1.Grandle官网下载Gradle 2.解压文件,配置到环境变量 3.测试安装成功,$ gradle -v 4.打开eclipse,Help-->Install new software,输 ...

  5. IOS中在自定义控件(非视图控制器)的视图跳转中 代理方法与代码块的比较

    //代码块与代替代理的设计方法 我就以在自定义视图中(非视图控制器,不能实现视图控制功能),通过代理和代码块两种方法分别实现视图的跳转,进行对比 首先自定义了一个视图,上面有一个已经注册了得BUtto ...

  6. SQLServer------解决IP地址登录不了数据库问题

    1.找到配置管理器,打开 2.关掉SQLExpress,打开MSSQLServer 3.配置MSSQLServer(启用Named Pipes和TCP/IP) 4.修改TCP/IP属性(端口:1433 ...

  7. win7开防火墙,允许别人远程

  8. SpringMVC中的设计模式

    1.<跟我学SpringMVC> P10 2.<跟我学SpringMVC> P32

  9. HTTP Status Code [RFC]

    来源:http://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml Hypertext Transfer Prot ...

  10. Sort 整理

    文章.图片参考:http://www.jianshu.com/p/1b4068ccd505?hmsr=toutiao.io&utm_medium=toutiao.io&utm_sour ...