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 ...
随机推荐
- OpenStack overview 笔记
Example architecture example architecture 至少需要两个节点启动一个虚拟机或者实例.可选的服务,例如Block storage和Object storage需要 ...
- Network client/server
<Beginning Linux Programming_4th> chapter 15 Sockets 1 A simple local client/server 1) clie ...
- POJ 2142 The Balance【扩展欧几里德】
题意:有两种类型的砝码,每种的砝码质量a和b给你,现在要求称出质量为c的物品,要求a的数量x和b的数量y最小,以及x+y的值最小. 用扩展欧几里德求ax+by=c,求出ax+by=1的一组通解,求出当 ...
- Spring 一二事(2)
静态工厂方法及实例工厂的使用: applicationContext.xml: <!-- factory-method 是指调用静态工厂方法 --> <bean id="h ...
- JavaWeb学习----http协议
一.什么是HTTP协议: 1.概念: 客户端连上web服务器后,若想获得web服务器中的某个web资源,需遵守一定的通讯格式,HTTP协议用于定义客户端与web服务器通迅的格式(规定客户端和服务器如何 ...
- java 19 -13 FIle类的一些方法2
package zl_file; import java.io.File; import java.io.IOException; import java.text.SimpleDateFormat; ...
- linux上的常见命令掌握
http://coolshell.cn/articles/8883.html 这篇文章来源于Quroa的一个问答<What are some time-saving tips that ever ...
- 首次安装Pycharm出现No Python interpreter selected解决方法
刚装完Pycharm,新建Project的时候,出现了No Python interpreter selected.网上的教程里path interpret栏里应该选中python.exe,但是我搜遍 ...
- 运维工作中常用到的几个rsync同步命令
作为一个运维工程师,经常可能会面对几十台.几百台甚至上千台服务器,除了批量操作外,环境同步.数据同步也是必不可少的技能.说到“同步”,不得不提的利器就是rsync. 下面结合本人近几年运维工作中对这一 ...
- iOS之block
1. Block的声明和线程安全Block属性的声明,首先需要用copy修饰符,因为只有copy后的Block才会在堆中,栈中的Block的生命周期是和栈绑定的,可以参考之前的文章(iOS: 非ARC ...