AC自动机-HDU2222-模板题
http://acm.hdu.edu.cn/showproblem.php?pid=2222
一个AC自动机的模板题。用的kuangbin的模板,静态建Trie树。可能遇到MLE的情况要转动态建树。
AC自动机的讲解看这里
http://blog.csdn.net/niushuai666/article/details/7002823
http://blog.csdn.net/mobius_strip/article/details/22549517
/*--------------------------------------------------------------------------------------*/
// Helica's header
// Second Editions
// 2015.11.7
//
#include <algorithm>
#include <iostream>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cstdio>
#include <vector>
#include <string>
#include <queue>
#include <stack>
#include <cmath>
#include <set>
#include <map> //debug function for a N*M array
#define debug_map(N,M,G) printf("\n");for(int i=0;i<(N);i++)\
{for(int j=;j<(M);j++){\
printf("%d",G[i][j]);}printf("\n");}
//debug function for int,float,double,etc.
#define debug_var(X) cout<<#X"="<<X<<endl;
/*--------------------------------------------------------------------------------------*/
using namespace std; int N,M,T; struct Trie
{
int next[][],fail[],end[];
int root,L;
int newnode()
{
for(int i=;i<;i++) next[L][i] = -;
end[L++] = ;
return L-;
}
void init()
{
L = ;
root = newnode();
} void insert(char *s)
{
int len = strlen(s);
int now = root;
for(int i=;i<len;i++)
{
if(next[now][s[i]-'a'] == -)
next[now][s[i]-'a'] = newnode();
now = next[now][s[i]-'a'];
}
end[now]++;
} void build()
{
queue <int> Q;
fail[root] = root;
for(int i=;i<;i++)
if(next[root][i] == -)
next[root][i] = root;
else
{
fail[next[root][i]] = root;
Q.push(next[root][i]);
} while(!Q.empty())
{
int now = Q.front();
Q.pop();
for(int i=;i<;i++)
{
if(next[now][i] == -)
next[now][i] = next[fail[now]][i];
else
{
fail[next[now][i]] = next[fail[now]][i];
Q.push(next[now][i]);
}
}
} } int query(char *s)
{
int len = strlen(s);
int now = root;
int ans = ;
for(int i=;i<len;i++)
{
now = next[now][s[i]-'a'];
int temp = now;
while(temp != root)
{
ans += end[temp];
end[temp] = ;
temp = fail[temp];
}
}
return ans;
}
void debug()
{
for(int i=;i<L;i++)
{
printf("id = %3d,fail = %3d,end = %3d,chi = [",i,fail[i],end[i]);
for(int j=;j<;j++)
printf("%2d",next[i][j]);
printf("]\n");
}
}
}; char buf[];
Trie ac; int main()
{
scanf("%d",&T);
while(T--)
{
ac.init();
scanf("%d",&N);
for(int i=;i<N;i++)
{
scanf("%s",buf);
ac.insert(buf);
}
ac.build();
//ac.debug();
scanf("%s",buf);
printf("%d\n",ac.query(buf));
}
}
第N次入门AC自动机。。。成功。。。
AC自动机-HDU2222-模板题的更多相关文章
- AC自动机-HDU2896-模板题
http://acm.hdu.edu.cn/showproblem.php?pid=2896 另一道AC自动机的模板题,不过这题需要记录一下具体的匹配情况. /*------------------- ...
- hdu 3695:Computer Virus on Planet Pandora(AC自动机,入门题)
Computer Virus on Planet Pandora Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 256000/1280 ...
- [hdu2222]ac自动机(模板)
题意:一个文本串+多个模板串的匹配问题 思路:裸的ac自动机. #pragma comment(linker, "/STACK:10240000,10240000") #inclu ...
- AC自动机 (模板)
AC自动机是用来干什么的: AC自动机是用来解决多模匹配问题,例如有单词s1,s2,s3,s4,s5,s6,问:在文本串ss中有几个单词出现过,类似. AC自动机实现这个功能需要三个部分: 1.将所有 ...
- AC自动机及其模板
模板 #include<queue> #include<stdio.h> #include<string.h> using namespace std; ; ; ; ...
- AC自动机(模板+例题)
首先要明白AC自动机是干什么的: AC自动机其实就是一种多模匹配算法,那么你可能会问什么叫做多模匹配算法.下面是我对多模匹配的理解,与多模与之对于的是单模,单模就是给你一个单词,然后给你一个字符串,问 ...
- HDOJ-3065(AC自动机+每个模板串的出现次数)
病毒侵袭持续中 HDOJ-3065 第一个需要注意的是树节点的个数也就是tree的第一维需要的空间是多少:模板串的个数*最长模板串的长度 一开始我的答案总时WA,原因是我的方法一开始不是这样做的,我是 ...
- AC自动机-HDU3065-简单题
http://acm.hdu.edu.cn/showproblem.php?pid=3065 需要记录匹配情况的AC自动机,没有清空一些数组导致wa了几发. /*------------------- ...
- luogu AC自动机(模板)
完全忘了AC自动机怎么写了qwq,更别说AC自动机上DP了. 今天就好好地学习字符串好了qwq 提一下AC自动机的时间复杂度--设n是模式串的个数,m是文本串的长度,l是模式串的平均长度,那么它的时间 ...
随机推荐
- MySQL(六)常用语法和数据类型
阅读MySQL语法时,需要注意的规则: ①符号用来指出几个选择中的一个,比如:null | not null表示或者给出null或者给出not null: ②包含在方括号中的关键字或子句(如[like ...
- 动手动脑(lesson 8)
一. 上面程序在不注释第一个i/j会出错,这是因为程序会顺序运行,在运行到try之前就已经出错,因此不会跳到异常处理. 异常处理基础知识: 二. 三. 运行结果: 运行结果: 四. 运行结果: 总结: ...
- SQLSERVER 中sp_who, sp_who2和sp_who3(转载)
sp_who可以返回如下信息: (可选参数LoginName, 或active代表活动会话数)Spid (系统进程ID)status (进程状态)loginame (用户登录名)hostname(用户 ...
- BZOJ2154/BZOJ2693/Luogu1829 Crash的数字表格/JZPFAR 莫比乌斯反演
传送门--Luogu 传送门--BZOJ2154 BZOJ2693是权限题 其中JZPFAR是多组询问,Crash的数字表格是单组询问 先推式子(默认\(N \leq M\),所有分数下取整) \(\ ...
- maven 单独构建多模块项目中的单个模块
maven 单独构建多模块项目中的单个模块, maven选项说明 -pl, --projects Build specified reactor projects instead of all pro ...
- WPF-利用Blend写的平面控制闸门开关动画
原文:WPF-利用Blend写的平面控制闸门开关动画 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/yangyisen0713/article/de ...
- jQuery checkbox全选 和全部取消
1.chkAll选中,全部chk选中 ,chkAll取消选中,全部chk取消选中 //chkAll选中,全部chk选中 ,chkAll取消选中,全部chk取消选中 $("#chkAll&q ...
- python 常见矩阵运算
python 的 numpy 库提供矩阵运算的功能,因此我们在需要矩阵运算的时候,需要导入 numpy 的包. 1.numpy 的导入和使用 from numpy import *;#导入numpy的 ...
- GATT服务搜索流程(二)
关于bta_dm_cb.p_sec_cback,这里我们之前已经分析过,他就是bte_dm_evt ,最终调用的函数btif_dm_upstreams_evt : static void btif_d ...
- 【亲测有效】Github无法访问或者访问速度的解决方案
我相信,很多朋友都遇到了 Github 访问速度过慢的问题,我也是在此记下笔记,方便以后拿来使用. 第一步.修改Hosts 通过问题的搜索了解到 github 访问很慢一般通过修改 hosts 文件解 ...