HDU 2222 Keywords Search (AC自动机)
题意:给你一些模式串,再给你一串匹配串,问你在匹配串中出现了多少种模式串,模式串可以相同
AC自动机:trie树上进行KMP。首先模式串建立trie树,再求得失配指针(类似next数组),其作用就是在这一位不匹配时转移到失配指针上。失配指针是转移到某个等于此位置最长后缀的位置,求法是bfs
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<vector>
#include<string>
#include<cstdio>
#include<cstring>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define eps 1E-8
/*注意可能会有输出-0.000*/
#define Sgn(x) (x<-eps? -1 :x<eps? 0:1)//x为两个浮点数差的比较,注意返回整型
#define Cvs(x) (x > 0.0 ? x+eps : x-eps)//浮点数转化
#define zero(x) (((x)>0?(x):-(x))<eps)//判断是否等于0
#define mul(a,b) (a<<b)
#define dir(a,b) (a>>b)
typedef long long ll;
typedef unsigned long long ull;
const int Inf=<<;
const double Pi=acos(-1.0);
const int Mod=1e9+;
const int Max=;
const int NumSize=;
struct node
{
int next[NumSize],coun;
int fail;//fail指针
void init()
{
memset(next,,sizeof(next));
coun=fail=;
}
} ACFA[Max];
int tot;
void Init()
{
tot=;
ACFA[tot].init();
return;
}
char str[Max];
void Insert(int len)//添加
{
int now=,mpos;
for(int i=; i<len; ++i)
{
mpos=str[i]-'a';
if(!ACFA[now].next[mpos])
{
ACFA[now].next[mpos]=++tot;
ACFA[tot].init();
}
now=ACFA[now].next[mpos];
}
ACFA[now].coun++;
return;
}
int que[Max];//bfs构造fail指针
void GetFail()//构造fail指针
{
int top=,bot=;
int now,fail;
for(int i=;i<NumSize;++i)//入队
if(ACFA[].next[i])
que[top++]=ACFA[].next[i];
while(top!=bot)
{
now=que[bot++];
for(int i=;i<NumSize;++i)
{
if(ACFA[now].next[i])
{
que[top++]=ACFA[now].next[i];
fail=ACFA[now].fail;
while(fail&&!ACFA[fail].next[i])//寻找失配指针位置
fail=ACFA[fail].fail;
if(ACFA[fail].next[i])//找到
fail=ACFA[fail].next[i];
ACFA[ACFA[now].next[i]].fail=fail;
}
else//建立trie图
ACFA[now].next[i]=ACFA[ACFA[now].fail].next[i];
}
}
return;
}
int Search(int len)
{
int ans=;
int mpos,nowp,now=;
for(int i=;i<len;++i)
{
mpos=str[i]-'a';
while(now>&&!ACFA[now].next[mpos])//失配后
now=ACFA[now].fail;
if(ACFA[now].next[mpos])//找到
{
now=ACFA[now].next[mpos];
nowp=now;
while(nowp&&ACFA[nowp].coun!=-)//-1找过
{
ans+=ACFA[nowp].coun;
ACFA[nowp].coun=-;
nowp=ACFA[nowp].fail;
}
}
}
return ans;
}
int main()
{
int t,n;
scanf("%d",&t);
while(t--)
{
Init();
scanf("%d",&n);
for(int i=; i<n; ++i)
{
scanf("%s",str);
Insert(strlen(str));
}
GetFail();
scanf("%s",str);
printf("%d\n",Search(strlen(str)));
}
return ;
}
HDU 2222 Keywords Search (AC自动机)的更多相关文章
- hdu 2222 Keywords Search——AC自动机
题目:http://acm.hdu.edu.cn/showproblem.php?pid=2222 第一道AC自动机! T了无数边后终于知道原来它是把若干询问串建一个自动机,把模式串放在上面跑:而且只 ...
- hdu 2222 Keywords Search ac自动机入门
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:有N(N <= 10000)个长度不超过50的模式串和一个长度不超过1e6的文本串. ...
- HDU 2222 Keywords Search(AC自动机模板题)
学习AC自动机请戳这里:大神blog........ 自动机的模板: #include <iostream> #include <algorithm> #include < ...
- HDU 2222 Keywords Search (AC自动机)
题意:就是求目标串中出现了几个模式串. 思路:用int型的end数组记录出现,AC自动机即可. #include<iostream> #include<cstdio> #inc ...
- hdu 2222 Keywords Search ac自动机模板
题目链接 先整理一发ac自动机模板.. #include <iostream> #include <vector> #include <cstdio> #inclu ...
- HDU 2222 Keywords Search (AC自动机)(模板题)
<题目链接> 题目大意: 给你一些单词,和一个字符串,问你这个字符串中含有多少个上面的单词. 解题分析: 这是多模匹配问题,如果用KMP的话,对每一个单词,都跑一遍KMP,那么当单词数量非 ...
- hdu 2222 Keywords Search - Aho-Corasick自动机
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submissio ...
- hdoj 2222 Keywords Search(AC自动机)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 思路分析:该问题为多模式匹配问题,使用AC自动机解决:需要注意的问题是如何统计该待查询的字符串包 ...
- hdu 2222 Keywords Search ac自己主动机
点击打开链接题目链接 Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Ja ...
- HDU 2222 Keywords Search AC自己主动机入门题
单词统计的题目,给出一些单词,统计有多少单词在一个文本中出现,最经典的入门题了. AC自己主动机的基础: 1 Trie. 以这个数据结构为基础的,只是添加一个fail指针和构造fail的函数 2 KM ...
随机推荐
- Python~if,while,for~顺序,判断,循环
if A: for -in : while x: if A:elif:else: 不能直接用int进行迭代,而必须加个range. range(len(L)) int ob ...
- XP共享连接数限制
- dhtmlxTree介绍(转载)
dhtmlxTree 是树菜单,允许我们快速开发界面优美,基于Ajax的javascript库. 她允许在线编辑,拖拽,三种状态(全选.不选.半选),复选框等模式.同时在加载大数据量的时候,仍然 可以 ...
- 如何手动添加Windows服务和如何把一个服务删除
windows 手动添加服务方法一:修改注册表 在注册表编辑器,展开分支"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services" ...
- NIS 报错No such map passwd.byname. Reason: Can't bind to server which serves this domain
在NIS—client端使用命令:ypcat passwd ,把错如上题, 原因:client端ypbind服务未启动解决方法:当然是启动ypbind了,命令:service ypbind start ...
- 【Excel 4.0 函数】REGISTER 的两种形式以及VBA等效语句
形式1 REGISTER("SAMPLE.DLL", "MyFunction", "AIC") 形式1等效 VBA语句 Declare Fu ...
- storyboard pushViewController 的时候,新的界面黑屏
storyboard 创建的一级界面需要通过代码跳转到另一 storyboard 创建的界面的时候,通常我们会这样 其实 alloc init 相当于重新创建一个界面,所以我们 push 进入之后会发 ...
- Android笔记:四大组件
1.Activity(是用户可以看到的主要的界面,使用时需要在AndroidManifest.xml中编辑声明.) 启动模式: 启动模式一共有四种,分别是standard.singleTop.sing ...
- 最小集合(51nod 1616)
A君有一个集合. 这个集合有个神奇的性质. 若X,Y属于该集合,那么X与Y的最大公因数也属于该集合. 但是他忘了这个集合中原先有哪些数字. 不过幸运的是,他记起了其中n个数字. 当然,或许会因为过度紧 ...
- 聊聊Android的APK反编译
上一篇<How To Use Proguard in Android APP>介绍了如何对Android进行混淆,现在来对它进行反编译看看,里面有些什么东西. APK文件,其实也是一个压缩 ...