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 ...
随机推荐
- 实用图像处理入门 - 2 - Windows平台下编译openCV
标签中的部分 font-family: 华文细黑; font-size: 26px; font-weight: bold; color: #611427; margin-top:40px; } h2 ...
- Eclipse中的快捷键总结
Eclipse中10个最有用的快捷键组合 一个Eclipse骨灰级开发者总结了他认为最有用但又不太为人所知的快捷键组合.通过这些组合可以更加容易的浏览源代码,使得整体的开发效率和质量得到提升. ...
- Redis 一二事 - 在spring中使用jedis 连接调试单机redis以及集群redis
Redis真是好,其中的键值用起来真心强大啊有木有, 之前的文章讲过搭建了redis集群 那么咋们该如何调用单机版的redis以及集群版的redis来使用缓存服务呢? 先讲讲单机版的,单机版redis ...
- java11-5 String类的转换功能
String的转换功能: byte[] getBytes():把字符串转换为字节数组. char[] toCharArray():把字符串转换为字符数组. static String valueOf( ...
- 图片button
- 【CSS】css网页背景图片设置
刚学CSS,了解了下网页背景图设置,顺便记录下. 下面主要是实现背景图位置保持不变,即不随滚动条动而动的功能. body { background-image:url(images/bck.png); ...
- 【转】【Asp.Net】asp.net(c#) 网页跳转
在asp.net下,经常需要页面的跳转,下面是具体的几种方法.跳转页面是大部编辑语言中都会有的,正面我们来分别介绍一下关于.net中response.redirect sever.execute se ...
- discourse 基于ember.js+rails项目的安装部署
最近公司在讨论做一个ERP运维问答的论坛系统,看了很多开源系统,觉得discourse功能比较完善,灵活.可配置性非常好,部署方便,瀑布流的主题布局模式也很符合未来论坛的趋势,于是在 ucloud 上 ...
- zabbix架构
监控系统: 数据的采集---存储---展示和分析---报警 snmp cacti:rrd agent nagios:不保存数据,mysql icmp/ssh ...
- [转]SRTM、ASTER GDEM等全球数字高程数据(DEM)下载方式简介
之前写过一篇短文对比过几种数字高程数据的区别:5种全球高程数据对比,这篇文章简要介绍下如何下载这些数据. 1.DLR的数字高程数据.该数据也是SRTM(shuttle radar topo ...