[hdu2222] [AC自动机模板] Keywords Search [AC自动机]
AC自动机模板,注意!ch,Fail,lab数组的大小不是n而是节点个数,需要认真计算!
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <queue> using namespace std; int cnt;
int ch[][],Fail[],lab[];
char str[],S[]; void Add(const char * s)
{
int p=,i;
for(i=;s[i];++i)
{
if(!ch[p][s[i]-])ch[p][s[i]-]=++cnt;
p=ch[p][s[i]-];
}
lab[p]++;
return ;
} void Build()
{
int i,t;
queue<int> Q;
Q.push();
while(!Q.empty())
{
t=Q.front();Q.pop();
for(i=;i<;++i)
{
if(ch[t][i])
{
Q.push(ch[t][i]);
Fail[ch[t][i]]=t?ch[Fail[t]][i]:;
}
else ch[t][i]=ch[Fail[t]][i];
}
}
return ;
} int main()
{
int T,n,i; scanf("%d",&T);
while(T--)
{
memset(Fail,,sizeof(Fail));
memset(ch,,sizeof(ch));
memset(lab,,sizeof(lab));
cnt=;
scanf("%d",&n);
for(i=;i<=n;++i)
{
scanf("%s",str+);
Add(str);
}
Build();
scanf("%s",S+);
int p=,Ans=;
for(i=;S[i];++i)
{
p=ch[p][S[i]-];
if(lab[p])
{
int pp=p;
while(pp)Ans+=lab[pp],lab[pp]=,pp=Fail[pp];
}
}
printf("%d\n",Ans);
} return ;
}
[hdu2222] [AC自动机模板] Keywords Search [AC自动机]的更多相关文章
- [AC自动机模板]Keywords Search
只是记录一下代码 AC自动机算法的教程请移步这里 还有这里 指针看着懵逼的还可以看一下这里 #include<iostream> #include<cstdio> #inclu ...
- HDU2222 Keywords Search [AC自动机模板]
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- 【HDU2222】Keywords Search AC自动机
[HDU2222]Keywords Search Problem Description In the modern time, Search engine came into the life of ...
- hdu2222 Keywords Search ac自动机
地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=2222 题目: Keywords Search Time Limit: 2000/1000 MS ...
- Keywords Search(AC自动机模板)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- HDU2222 Keywords Search —— AC自动机
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 Keywords Search Time Limit: 2000/1000 MS (Java/O ...
- Match:Keywords Search(AC自动机模板)(HDU 2222)
多模匹配 题目大意:给定很多个字串A,B,C,D,E....,然后再给你目标串str字串,看目标串中出现多少个给定的字串. 经典AC自动机模板题,不多说. #include <iostream& ...
- hdu 2222 Keywords Search ac自动机模板
题目链接 先整理一发ac自动机模板.. #include <iostream> #include <vector> #include <cstdio> #inclu ...
- POJ2222 Keywords Search AC自动机模板
http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:给出一些单词,求多少个单词在字符串中出现过(单词表单词可能有相同的,这些相同的单词视为不同的分别计数 ...
随机推荐
- PropertyInfo 类
[AttributeUsage(AttributeTargets.Property)] //Models 特性 public class CanWriteAttribute : Attr ...
- T4模板使用记录,生成Model、Service、Repository
自己目前在搭建一个.NET Core的框架,本来是打算使用前端做代码生成器直接生成到文件的,快做好了.感觉好像使用T4更方便一些,所以也就有了这篇文章~ 我还是有个问题没解决,就是我想生成每个类(接 ...
- 执行update, insert,delete 语句, 不返回结果集,(类型化参数)
/// <summary> /// 执行update, insert,delete 语句, 不返回结果集,(类型化参数) /// </summary> /// <para ...
- Unity3d dotween
位置 1. 移动到指定位置 obj.transform.DOMove(, , ), 2f); a. 单方向可以用DOMoveX.DOMoveY.DOMoveZ b. 本地坐标系版本:DOLocalMo ...
- 超经典~超全的jQuery插件大全
海量的jQuery插件帖,很经典,不知道什么时候开始流传,很早以前就收藏过,为了工作方便还是发了一份放在日志里面. 其中有些已经无法访问,或许是文件移除,或许是被封锁.大家分享的东西,没什么特别的可说 ...
- js技巧(二)
1.封装获取id: function show(Id){ var aa=document.getElementById(Id); return aa; } 调用:console.log(show(&q ...
- boolean b=true?false:true==true?false:true;
下列代码的输出结果是_____ boolean b=true?false:true==true?false:true;System.out.println(b); 答案:false 题目来源:携程20 ...
- SQL基本操作——declare if lese while
declare --第一种 declare @i int set @i= (select COUNT(*) from t8) select @i --第二种 declare @i int select ...
- java实现搜索附近地点或人的功能
前言 当前大多数app都有查找附近的功能, 简单的有查找周围的运动场馆, 复杂的有滴滴, 摩拜查找周围的车辆. 本文主要阐述查找附近地点的一般实现. 方案比较 方案1 (性能还不错) 数据库直接存经纬 ...
- dubbo之延迟连接及粘滞链接接
延迟连接 延迟连接用于减少长连接数.当有调用发起时,再创建长连接.1 <dubbo:protocol name="dubbo" lazy="true" / ...