HDU2222【AC自动机(基础·模板)】
Frist AC zi dong ji(Aho-Corasick Automation) of life
#include <bits/stdc++.h>
using namespace std; const int N=5e5+10; //10000个串,长度为50 struct Trie{
int num;
Trie *next[27],*fail;
};
Trie q[N],*root;
int tol; Trie* Creat()
{
Trie *p;
p=&q[tol++];
p->fail=NULL;
p->num=0;
for(int i=0;i<26;i++)
p->next[i]=NULL;
return p;
} void Insert(char *str)
{
Trie *p=root;
int index,len=strlen(str);
for(int i=0;i<len;i++)
{
index=str[i]-'a';
if(p->next[index]==NULL)
p->next[index]=Creat();
p=p->next[index];
}
p->num++;
} void Build_Ac()
{
queue<Trie*>que;
que.push(root);
while(!que.empty())
{
Trie *p=que.front();que.pop();
for(int i=0;i<26;i++)
{
if(p->next[i]!=NULL)
{
if(p==root)
p->next[i]->fail=root;
else
{
Trie *temp;
temp=p->fail;
while(temp!=NULL)
{
if(temp->next[i]!=NULL)
{
p->next[i]->fail=temp->next[i];
break;
}
temp=temp->fail;
}
if(temp==NULL)
p->next[i]->fail=root;
}
que.push(p->next[i]);
}
}
}
} char word[1000010];
int Query()
{
int ans,index,len;
Trie *p=root;
ans=0;
len=strlen(word);
for(int i=0;i<len;i++)
{
index=word[i]-'a';
while(p->next[index]==NULL && p!=root)//失配跳转到失败指针
p=p->fail;
p=p->next[index];
if(p==NULL) //还是失配
p=root;
Trie *temp=p; //p不动,temp计算后缀串
while(temp!=root && temp->num!=-1)
{
ans+=temp->num;
temp->num=-1;
temp=temp->fail;
}
}
return ans;
} int main()
{
char s[51];
int T;
scanf("%d",&T);
while(T--)
{
int n;
tol=0;
root=Creat();
scanf("%d",&n);
while(n--)
{
scanf("%s",s);
Insert(s);
}
Build_Ac();
scanf("%s",word);
printf("%d\n",Query());
}
return 0;
}
HDU2222【AC自动机(基础·模板)】的更多相关文章
- [hdu2222]ac自动机(模板)
题意:一个文本串+多个模板串的匹配问题 思路:裸的ac自动机. #pragma comment(linker, "/STACK:10240000,10240000") #inclu ...
- hdu2222 ac自动机入门
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- [hdu2222] [AC自动机模板] Keywords Search [AC自动机]
AC自动机模板,注意!ch,Fail,lab数组的大小不是n而是节点个数,需要认真计算! #include <iostream> #include <algorithm> #i ...
- Keywords Search HDU2222 AC自动机模板题
ac自动机说起来很复杂,其实和kmp是一样的思路,都是寻找相同前后缀,减少跳的次数.只要理解了kmp是怎么求next数组的,ac自动机bfs甚至比knp还好写. 这里大致说一下kmp求next数组的方 ...
- AC自动机基础知识讲解
AC自动机 转载自:小白 还可参考:飘过的小牛 1.KMP算法: a. 传统字符串的匹配和KMP: 对于字符串S = ”abcabcabdabba”,T = ”abcabd”,如果用T去匹配S下划线部 ...
- HDU2222 (AC自动机)
AC自动机模板题. 被卡内存了 死活A不掉.. AC自动机参考教程: http://www.cppblog.com/menjitianya/archive/2014/07/10/207604.html ...
- HDU2222(AC自动机入门题)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- AC自动机(模板+例题)
首先要明白AC自动机是干什么的: AC自动机其实就是一种多模匹配算法,那么你可能会问什么叫做多模匹配算法.下面是我对多模匹配的理解,与多模与之对于的是单模,单模就是给你一个单词,然后给你一个字符串,问 ...
- AC自动机 (模板)
AC自动机是用来干什么的: AC自动机是用来解决多模匹配问题,例如有单词s1,s2,s3,s4,s5,s6,问:在文本串ss中有几个单词出现过,类似. AC自动机实现这个功能需要三个部分: 1.将所有 ...
- HDOJ-3065(AC自动机+每个模板串的出现次数)
病毒侵袭持续中 HDOJ-3065 第一个需要注意的是树节点的个数也就是tree的第一维需要的空间是多少:模板串的个数*最长模板串的长度 一开始我的答案总时WA,原因是我的方法一开始不是这样做的,我是 ...
随机推荐
- 各种数据库(oracle、mysql、sqlserver等)在Spring中数据源的配置和JDBC驱动包----转
在开发基于数据库的应用系统时,需要在项目中进行数据源的配置来为数据 库的操作取得数据库连接.配置不同数据库的数据源的方法大体上都是相同的,不同的只是不同数据库的JDBC驱动类和连接URL以及相应的数据 ...
- BZOJ3627: [JLOI2014]路径规划
BZOJ3627: [JLOI2014]路径规划 Description 相信大家都用过地图上的路径规划功能,只要输入起点终点就能找出一条最优路线.现在告诉你一张地图的信息,请你找出最优路径(即最短路 ...
- Sqoop hive导出到mysql[转]
通过Sqoop将Hive表数据导入到MySQL通常有两种情况. 第一种是将hive上某张表的全部数据导入到mysql对应的表中. 第二种是将hive上某张表中的部分数据导入到mysql对应的表中. 两 ...
- matlab从fig文件中提取数据
如果你的fig文件中图像是由多条曲线绘制而成,比如说plot命令生成的,通过以下方式输出横坐标,纵坐标的取值 open('figname.fig'); lh = findall(gca, 'type' ...
- Ubuntu 14.04 下 android studio 安装 和 配置【转】
本文转载自:http://blog.csdn.net/xueshanfeihu0/article/details/52979717 Ubuntu 14.04 下 android studio 安装 和 ...
- jquery特效(7)—弹出遮罩层且内容居中
上周写了几个小特效,其中有个点击按钮弹出遮罩层的特效,下面来看最终实现的效果: 由于是测试的程序,所以我未加关闭的按钮. 一.主体程序 <!DOCTYPE html> <html&g ...
- 【转】hibernate懒加载的问题,failed to lazily initialize a collection of role
hibernate懒加载的问题,failed to lazily initialize a collection of role hibernate懒加载的问题,failed to lazily in ...
- iOS description
description:重写对象的这个方法,会在打印的时候显示出自定义的description中的内容debugDescription:方法是在开发者在调试器中以控制台命令打印对象时才调用的. 在NS ...
- java编程思想-基础
interface: 方法默认为public:成员变量默认 static and final 对象数组的定义:理解? 多接口继承:可以多个接口,但只有一个具体类,具体类在前面 自:多接口继承时,来自不 ...
- poj 3617 Best Cow Line 解题报告
题目链接:http://poj.org/problem?id=3617 题目意思:给出一条长度为n的字符串S,目标是要构造一条字典序尽量小,长度为n的字符串T.构造的规则是,如果S的头部的字母 < ...