hdu2222 AC自动机
字典树也可以做。
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define maxn 1000010
struct ac_mation
{
ac_mation *fail;//失败指针
ac_mation *next[];//26个字母
int flag;//标记是否为最后一个点
init(){//函数初始化
flag=;
fail=NULL;
for(int i=;i<;i++)
next[i]=NULL;
}
}*q[];
ac_mation *root;
char s[maxn],str[];
int n,head,tail;
void insert(char *str)
{
int i,j,len=strlen(str);
ac_mation *p=root,*qq;
for(i=;i<len;i++)
{
int id=str[i]-'a';
if(p->next[id]==NULL)
{
qq=new ac_mation;
qq->init();
p->next[id]=qq;
}
p=p->next[id];
}
p->flag++;
}
void getfail()//构建失败指针
{
int i;
q[tail++]=root;
while(head!=tail)//bfs
{
ac_mation *p=q[head++];
ac_mation *temp=NULL;
for(i=;i<;i++)
{
if(p->next[i]!=NULL)
{
if(p==root)//第一个元素的fail指针必须是root
p->next[i]->fail=root;
else
{
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;
}
q[tail++]=p->next[i];
}
}
}
}
void query(char *s)
{
int ans=;
int i,j,len=strlen(s);
ac_mation *p=root,*temp;
for(i=;i<len;i++)
{
int id=s[i]-'a';
while(p->next[id]==NULL&&p!=root)//跳转到失败指针
p=p->fail;
p=p->next[id];
if(p==NULL)
p=root;
temp=p;
while(temp!=root&&temp->flag!=-)
{
ans+=temp->flag;
temp->flag=-;
temp=temp->fail;
}
}
printf("%d\n",ans);
}
void freeac_mation(ac_mation *root)
{
int i;
for(i=;i<;i++)
{
if(root->next[i])
freeac_mation(root->next[i]);
}
free(root);
}
int main()
{
int i,t,j;
scanf("%d",&t);
while(t--)
{
head=tail=;
root=new ac_mation;
root->init();
scanf("%d",&n);
for(i=;i<n;i++)
{
scanf("%s",str);
insert(str);
}
getfail();
scanf("%s",s);
query(s);
freeac_mation(root);
}
}
hdu2222 AC自动机的更多相关文章
- 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: 65536/32768 K (Java/Others) ...
- HDU2222(AC自动机入门题)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 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数组的方 ...
- [hdu2222]ac自动机(模板)
题意:一个文本串+多个模板串的匹配问题 思路:裸的ac自动机. #pragma comment(linker, "/STACK:10240000,10240000") #inclu ...
- hdu2222(ac自动机模板)
#include<iostream> #include<cmath> #include<cstdio> #include<cstring> #inclu ...
- 【HDU2222】Keywords Search AC自动机
[HDU2222]Keywords Search Problem Description In the modern time, Search engine came into the life of ...
- 【HDU2222】Keywords Search(AC自动机)
Problem Description In the modern time, Search engine came into the life of everybody like Google, B ...
随机推荐
- 分享十二个有用的jQuery代码
分享7个有用的jQuery代码 这篇文章主要介绍了7个有用的jQuery技巧分享,本文给出了在新窗口打开链接.设置等高的列.jQuery预加载图像.禁用鼠标右键.设定计时器等实用代码片段,需要的朋友可 ...
- 深度优先搜索 codevs 1064 虫食算
codevs 1064 虫食算 2004年NOIP全国联赛提高组 时间限制: 2 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description 所 ...
- RabbitMQ - 实例操作
以前在单项目中用过RabbitMQ,没有问题 不过这次在分布式项目中使用RabbitMQ中有点搞糊涂了,但是实际上是没有问题的,思路清晰就行 简单看一下实际操作的示例吧: 资源文件中需要配置基本的ra ...
- ios app架构设计系统文章
三. iOS应用架构谈(三):网络层设计方案(上) http://www.infoq.com/cn/articles/ios-app-arch-3-1?utm_source=infoq&utm ...
- ABP入门系列(6)——展现层实现增删改查
这一章节将通过完善Controller.View.ViewModel,来实现展现层的增删改查.最终实现效果如下图: 一.定义Controller ABP对ASP.NET MVC Controllers ...
- 使用CuteSlider做网站炫酷的幻灯片
cuteSlider 1.预览 官网:http://www.cuteslider.com/ 应用:http://www.dutphonelab.org/ 2.资料 文档:http://pan.baid ...
- React Native iOS环境搭建
前段时间React Native for Android发布,感觉React Native会越来越多的公司开始研究.使用.所以周六也抽空搭建了iOS的开发环境,以便以后利用空闲的时间能够学习一下. 废 ...
- 第二章 时间控件(DateTime Picker)
这家伙太懒了,碰到问题才写博文,嘿嘿. 好了进入正题,二话不说,先放地址: 中文:http://www.bootcss.com/p/bootstrap-datetimepicker/index.htm ...
- 通过spring,在项目的任意位置获取当前Request
需要引入: import javax.servlet.http.HttpServletRequest; import org.springframework.web.context.request.R ...
- Table AdvanceTable针对表内行禁用multipleSelection , singleSelection
OATableBean table = (OATableBean)webBean.findChildRecursive("LineTable"); table.setInserta ...