HDU 2222 AC自动机模板题
题目: 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自动机模板题的更多相关文章
- HDU 3065 (AC自动机模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3065 题目大意:多个模式串,范围是大写字母.匹配串的字符范围是(0~127).问匹配串中含有哪几种模 ...
- HDU 2896 (AC自动机模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2896 题目大意:多个模式串.多个匹配串.其中串的字符范围是(0~127).问匹配串中含有哪几个模式串 ...
- hdu 2222(AC自动机模版题)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- HDU 2222 AC自动机 裸题
题意: 问母串中出现多少个模式串 注意ac自动机的节点总数 #include <stdio.h> #include <string.h> #include <queue& ...
- HDU 2222 AC自动机模版题
所学的AC自动机都源于斌哥和昀神的想法. 题意:求目标串中出现了几个模式串. 使用一个int型的end数组记录,查询一次. #include <cstdio> #include <c ...
- HDU 2222 & ac自动机模板
题意: 求n个模板串在匹配串中出现了几个. SOL: 反正就是模板啦...似乎比KMP都简单----这么说似乎有点不道德...毕竟先看的KMP而他们并没有什么不同... 貌似自己的理解和他们画的图还是 ...
- HDU 2222 AC自动机(模版题)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- Keywords Search HDU - 2222 AC自动机板子题
In the modern time, Search engine came into the life of everybody like Google, Baidu, etc. Wiskey al ...
- HDU 2222(AC自动机模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2222 题目大意:多个模式串.问匹配串中含有多少个模式串.注意模式串有重复,所以要累计重复结果. 解题 ...
随机推荐
- UVA12034Race(DP)
题目链接 题意:A.B两人赛马,最终名次有三种可能:并列第一:A第一,B第二:B第一,A第二.输入n,求 n 人赛马时最终名次的可能,结果对10056取余 分析:对于 i 匹马来说,有 i 中可能,设 ...
- O(1)快速乘注意事项
O(1)快速乘是经典玄学优化啦~由于刚挂了一次特此总结一番. ll mul(ll u,ll v){ return(u*v-ll((long double)u*v/p)*p+p)%p; } double ...
- WinForm------TreeList加载数据方法
1.SQLService操作 (1)在SQLServer创建一张表dbo.Department (2)写入以下数据 2.VS操作(这里如何使用EntityFramework加载数据库就不多说了哈) ( ...
- Linux环境VNC服务安装、配置与使用
前言:作为一名DBA,在创建Oracle数据库的过程中一般要使用dbca和netca图像化进行建库和创建监听(如果使用脚本建库另说),如果您身体好估计可以在瑟瑟发抖的机房中完成数据库的创建过程,由于本 ...
- 9月19日下午JavaScript数组冒泡排列和二分法
数组 一.冒泡排列 对数组attr = [1,8,6,4,5,3,7,2,9]进行由大到小排列,用冒泡排列的方法排列时,会对数组进行比较互换.如果前一个数字较大,这2个元素排列方式不变,如果后一个元素 ...
- VclZip压缩文件夹
压缩指定路径MyZipDir下的文件夹b及b目录下的所有文件和文件b.txt function ZipDir(zipMode:Integer;zipControl:TVCLZip;MyZipName, ...
- Java I/O流体系
- Python + OpenCV2 系列:2 - 图片操作
这些相当于我的学习笔记,所以并没有很强的结构性和很全的介绍,请见谅. 1 读取.写入图像 下面是一个简短的载入图像.打印尺寸.转换格式及保存图像为.png的例子: # -*- coding: utf- ...
- jar tvf study.war jar命令查看war/jar包的内容
jar tvf study.war 0 Thu Oct 20 14:01:18 CST 2016 META-INF/ 137 Thu Oct 20 14:01:16 CST 2016 META-INF ...
- Orchard源码分析(3):Orchard.WarmupStarter程序集
概述 Orchard.WarmupStarter程序集包含三个类:WarmupUtility.WarmupHttpModule和Starter<T>.该程序集主要为Orchard应用启动初 ...