字典树也可以做。

#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自动机的更多相关文章

  1. HDU2222 (AC自动机)

    AC自动机模板题. 被卡内存了 死活A不掉.. AC自动机参考教程: http://www.cppblog.com/menjitianya/archive/2014/07/10/207604.html ...

  2. hdu2222 ac自动机入门

    Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  3. HDU2222(AC自动机入门题)

    Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others ...

  4. [hdu2222] [AC自动机模板] Keywords Search [AC自动机]

    AC自动机模板,注意!ch,Fail,lab数组的大小不是n而是节点个数,需要认真计算! #include <iostream> #include <algorithm> #i ...

  5. Keywords Search HDU2222 AC自动机模板题

    ac自动机说起来很复杂,其实和kmp是一样的思路,都是寻找相同前后缀,减少跳的次数.只要理解了kmp是怎么求next数组的,ac自动机bfs甚至比knp还好写. 这里大致说一下kmp求next数组的方 ...

  6. [hdu2222]ac自动机(模板)

    题意:一个文本串+多个模板串的匹配问题 思路:裸的ac自动机. #pragma comment(linker, "/STACK:10240000,10240000") #inclu ...

  7. hdu2222(ac自动机模板)

    #include<iostream> #include<cmath> #include<cstdio> #include<cstring> #inclu ...

  8. 【HDU2222】Keywords Search AC自动机

    [HDU2222]Keywords Search Problem Description In the modern time, Search engine came into the life of ...

  9. 【HDU2222】Keywords Search(AC自动机)

    Problem Description In the modern time, Search engine came into the life of everybody like Google, B ...

随机推荐

  1. js/jquery判断浏览器的方法总结

    JS获取浏览器信息浏览器代码名称:navigator.appCodeName浏览器名称:navigator.appName浏览器版本号:navigator.appVersion对Java的支持:nav ...

  2. 如何修复AppScan漏洞

    [AppScan]修复漏洞一:启用不安全的HTTP方法 (中) 漏洞背景:      “启用了不安全的 HTTP 方法”属于“中”危漏洞.漏洞描述是:根据APPSCAN的报告,APPSCAN通过OPT ...

  3. java 14 -7 Date

    Date:表示特定的瞬间,精确到毫秒. 了解了解就行. 已经过时,在 JDK 1.1 之前,类 Date 有两个其他的函数.它允许把日期解释为年.月.日.小时.分钟和秒值.它也允许格式化和解析日期字符 ...

  4. Ember模板中的操作指向

    模板中的链接操作指向有三个地方,该模板对应的控制器和路由以及视图,默认是先跳转到控制器,如果控制器里没有定义模板中动作的方法,就去该模板对应的路由里找,如果还没找到,就去父级路由找,直到顶级路由,如果 ...

  5. 18Spring_AOP编程(AspectJ)_AspectJ的各种通知总结

    小结: 前置通知(权限控制). 后置通知 ---- 不怎么用 环绕通知(权限控制. 性能监控. 缓存技术 ) 异常通知 (发生异常后, 记录错误日志 ) 最终通知 (释放资源 ) 环绕通知 是取代任何 ...

  6. [转]一个四叉树Demo学习

    程序代码: http://www.codeproject.com/Articles/30535/A-Simple-QuadTree-Implementation-in-C 四叉树: using Sys ...

  7. php基础23:数值与数值函数

    <?php //1.自动转换 $a = 5; $b = "5a"; $c = $a + $b; echo $c; echo "<br>"; $ ...

  8. 基于EventAggregator的事件发布及订阅

    EventAggregator简介 EventAggregator是Prism中专门处理ViewModel与ViewModel之间事件传递的类对象,它提供了针对事件的发布方法和订阅方法,所以可以非常方 ...

  9. Caffe学习系列(21):caffe图形化操作工具digits的安装与运行

    经过前面一系列的学习,我们基本上学会了如何在linux下运行caffe程序,也学会了如何用python接口进行数据及参数的可视化. 如果还没有学会的,请自行细细阅读: caffe学习系列:http:/ ...

  10. 浪潮之巅——IT那点事之二—蓝色巨人IBM

    蓝色巨人IBM的全称是国际商用机器公司(International Business Machines Corporation),纵观IBM的发展历史,可以看出IBM与机器结缘,以商用为主的发展策略. ...