题目: 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. primefaces p:dataExporter filename 支持中文 utf8

    p:fileDownload and p:dataExporter : for p:fileDownload, the Content-Disposition header should be set ...

  2. ionic中获取坐标方法

    ionic中获取坐标的方法 1.首相需要执行命令: cordova plugin add cordova-plugin-geolocation2.然后块级注入配置bower文件引入ngCordova ...

  3. 9.19 JS数组

    数组:相同类型数据的集合强类型语言:1数组里面只能存放相同数据类型的数据     2定义数组的时候需要制定一个长度(可以存放的元素数量)     3内存空间连续集合:1.可以存放任意类型的数据     ...

  4. 转:在VS2010下编译、调试和生成mex文件

    最近帮人调了一个程序,是网上公开的代码,利用matlab与c++混合编程做三维模型关键点检测,发现他们可以用VS2010编译.调试.生成mexw32文件,因此觉得之前在Matlab上利用mex命令真是 ...

  5. photobooth.js

    HTML5 Webcam for your website, photobooth.js

  6. Oracle 的表备份的方法

    1.直接备份(防止误操作后数据库表不能恢复) create table new_table as select * from old_table; 2.创建表头,然后插入列(繁琐的做法) create ...

  7. 对象Clone

    //================================================= // File Name : Clone_demo //-------------------- ...

  8. Yoshua Bengio 2016年5月11日在Twitter Boston的演讲PPT

    Yoshua Bengio最新演讲:Attention 让深度学习取得巨大成功(46ppt) Yoshua Bengio,电脑科学家,毕业于麦吉尔大学,在MIT和AT&T贝尔实验室做过博士后研 ...

  9. linux设置和删除环境变量

    删除环境变量: unset -v PKG_CONFIG_PATH 添加环境变量: export PKG_CONFIG_PATH="你需要设置的路径", 例如: export LAN ...

  10. 字体投影(test-shadow)与框架投影(box-shadow)

    字体投影:text-shadow: 1px 1px 1px #ccc; 框架投影:box-shadow: 10px 10px 25px #ccc;