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 ...
随机推荐
- HDU 4409 Family Name List --乱搞、LCA
题意: 给出一些名字,名字间有父子关系,有三种操作: 1.按祖先到后代,兄弟间按字典序由小到大排序,然后输出 2.求某个节点的兄弟节点有多少个,包括自己(注意,根节点的兄弟节点是1) 3.求节点a和b ...
- 常用Eclipse插件在线安装地址
Srping IDE http://www.springsource.com/update/e3.5 EasyShellhttp://pluginbox.sourceforge.net M2E ...
- java11-2 String面试题
package cn.itcast_02; /* * String s = new String(“hello”)和String s = “hello”;的区别? * 有.前者会创建2个对象,后者创建 ...
- 18Mybatis_动态sql_foreach
foreach: 向sql传递数组或List,mybatis使用foreach解析 应用场景: 在用户查询列表和查询总数的statement中增加多个id输入查询. sql语句如下: 两种方法: SE ...
- jqXHR 对象(post完成后再调用函数)
场景: function A() { B(); C(); } function B() { $.post(url, {}, function () { alert("我错了!"); ...
- [每日自动更新]Hillstone 山石网科 StoneOS ISP路由表配置文件
1.数据基于APNIC,准确有效 2.适用于StoneOS 4.0~5.5各版本 3.对APNIC数据进行路由聚合,实现最小子网 4.覆盖中国大陆地区电信.联通.移动三大运营商,长宽.电信通等二级运营 ...
- 双绞线线序+POE供电网线
0 重点 一般情况下会用1236(橙白.橙.绿白.绿)传输数据,1.2用于发送,3.6用于接收,45(蓝.蓝白)电源正极 78(棕白.棕)电源负极. 一 网线线序 12发 36收 二 poe网线供电 ...
- How to get date from OAMessageDateFieldBean
OAMessageDateFieldBean dateFromBean = (OAMessageDateFieldBean)webBean.findChildRecursive("pRece ...
- 软件开发之路、Step 1 需求分析
百度百科 需求分析 所谓"需求分析",是指对要解决的问题进行详细的分析,弄清楚问题的要求,包括需要输入什么数据,要得到什么结果,最后应输出什么.可以说,在软件工程当中的“需求分析” ...
- [vim]的关键字补全
除了complete关键字补全,所有补全相关命令都以CTRL-X开始,然后再接与补全类型相关的命令.CTRL-N与CTRL-P在找的的内容中选择的通用的命令,上下选择用的,CTRL-E则是取消选择.( ...