【HDU2222】

最纯粹的裸题,错误点详见注释。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
struct ACauto
{
int sum;
ACauto* next[];
ACauto* fail;
ACauto()
{
sum=;
for (int i=;i<;i++) next[i]=NULL;
fail=NULL;
}
}; void insert(ACauto* root,char* str)
{
int len=strlen(str);
ACauto* now=root;
for (int i=;i<len;i++)
{
int index=str[i]-'a';
if (now->next[index]==NULL)
{
ACauto* tmp=new ACauto;
now->next[index]=tmp;
}
now=now->next[index];
}
now->sum++;
} void build(ACauto* root)
{
queue<ACauto*> que;
que.push(root);
while (!que.empty())
{
ACauto* tmp=que.front();que.pop();
for (int i=;i<;i++)
{
if (tmp->next[i]==NULL) continue;
if (tmp==root)
tmp->next[i]->fail=root;
else
{
ACauto* p=tmp->fail;
while (p!=NULL)
{
if (p->next[i]!=NULL)
{
tmp->next[i]->fail=p->next[i];
break;
}
p=p->fail;
}
if (p==NULL) tmp->next[i]->fail=root;
}
que.push(tmp->next[i]);
}
}
} int query(ACauto* root,char* str)
{
int ans=,len=strlen(str);
ACauto* p=root;
for (int i=;i<len;i++)
{
int index=str[i]-'a';
while (p->next[index]==NULL && p!=root) p=p->fail;
/*错误点:上述语句是while语句不是if语句*/
p=p->next[index];
p=(p==NULL)?root:p;
ACauto* tmp=p;
while (tmp!=root)
/*-1表示这个单词之前已经被统计过了,不再重复计算*/
{
if (tmp->sum>=)
{
ans+=tmp->sum;
tmp->sum=-;
}
else
break;
tmp=tmp->fail;
}
}
return ans;
} void submain()
{
ACauto* root=new ACauto;
int n;
char str[+];
scanf("%d",&n);
for (int i=;i<n;i++)
{
scanf("%s",str);
insert(root,str);
}
build(root);
scanf("%s",str);
cout<<query(root,str)<<endl;
} int main()
{
int T;
scanf("%d",&T);
for (int kase=;kase<T;kase++) submain();
return ;
}

【AC自动机】HDU中模板题的更多相关文章

  1. hdu5384 AC自己主动机模板题,统计模式串在给定串中出现的个数

    http://acm.hdu.edu.cn/showproblem.php?pid=5384 Problem Description Danganronpa is a video game franc ...

  2. NYOJ 1085 数单词 (AC自己主动机模板题)

    数单词 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描写叙述 为了可以顺利通过英语四六级考试,如今大家每天早上都会早起读英语. LYH本来以为自己在6月份的考试中能够通过六 ...

  3. 【HDU】病毒侵袭(AC自己主动机模板题)

    AC自己主动机的模板题.因为输入的字符串中的字符不保证全为小写字母.所以范围应该在130之前,而前31位字符是不可能出如今字符串的(不懂得查下ACSII表即可了).所以仅仅须要开的结点数组大小为130 ...

  4. 数据结构--AC自动机--hdu 2896

    病毒侵袭 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  5. AC自动机 HDU 2896

    n个字串 m个母串 字串在母串中出现几次 #include<stdio.h> #include<algorithm> #include<string.h> #inc ...

  6. 从0开始 数据结构 AC自动机 hdu 2222

    参考博客 失配指针原理 使当前字符失配时跳转到另一段从root开始每一个字符都与当前已匹配字符段某一个后缀完全相同且长度最大的位置继续匹配,如同KMP算法一样,AC自动机在匹配时如果当前字符串匹配失败 ...

  7. AC自动机 HDU 3065

    大概就是裸的AC自动机了 #include<stdio.h> #include<algorithm> #include<string.h> #include< ...

  8. HDU 2222 Keywords Search(AC自己主动机模板题)

    题意:给出一个字符串和若干个模板,求出在文本串中出现的模板个数. 思路:由于有可能有反复的模板,trie树权值记录每一个模板出现的次数就可以. #include<cstdio> #incl ...

  9. HDU 5384 Danganronpa (AC自己主动机模板题)

    题意:给出n个文本和m个模板.求每一个文本中全部模板出现的总次数. 思路:Trie树权值记录每一个模板的个数.对于每一个文本跑一边find就可以. #include<cstdio> #in ...

随机推荐

  1. fundamentals of the jQuery library

    1.why is jquery Only 32kB minified and gzipped. Can also be included as an AMD module Supports CSS3 ...

  2. Fiddler-- 安装HTTPs证书

    1. 现在很多带有比较重要信息的接口都使用了安全性更高的HTTPS,而Fiddler默认是抓取HTTP类型的接口,要想查看HTTPS类型接口就需要安装fiddler证书.   2.打开Fiddler, ...

  3. Msfvenom学习总结-MSF反弹webshell

    1.    –p (- -payload-options) 添加载荷payload. 载荷这个东西比较多,这个软件就是根据对应的载荷payload生成对应平台下的后门,所以只有选对payload,再填 ...

  4. 【NOIP2016】补题

    今天突然想到自己居然还没把NOIP2016补完 简直是傻逼... 所以开始写 D1T1:模拟 D1T2:NOIP最难的一道题,首先求LCA 离线下,把观察员单独提出来 然后可以维护一个类似桶排序的东西 ...

  5. C后端设计开发 - 第3章-气功-原子锁线程协程

    正文 第3章-气功-原子锁线程协程 后记 如果有错误, 欢迎指正. 有好的补充, 和疑问欢迎交流, 一块提高. 在此谢谢大家了. 童话镇 - http://music.163.com/#/m/song ...

  6. [caffe error] undefined reference to `inflateValidate@ZLIB_1.2.9'

    undefined reference to `inflateValidate@ZLIB_1.2.9' Makefile.config添加一行LINKFLAGS := -Wl,-rpath,$(HOM ...

  7. centos7.4关闭防火前

    systemctl stop firewalld.service #停止firewallsystemctl disable firewalld.service #禁止firewall开机启动firew ...

  8. Hibernate检索策略与检索方式

    hibernate的Session在加载Java对象时,一般都会把鱼这个对象相关联的其他Java对象也都加载到缓存中,以方便程序的调用.但很多情况下,我们不需要加载太多无用的对象到缓存中,一来会占用大 ...

  9. mac系统安装mysql

    1.下载 打开官网:https://www.mysql.com 进入DOWNLOADS--->Community--->MySQL Community Server--->DOWNL ...

  10. thinkphp5 消息队列thinkphp-queue扩展

    1.简介 thinkphp-queue是thinkphp的一个第三方扩展, 内置了 Redis,Database,Topthink ,Sync这四种驱动,推荐使用redis 2. 下载 和安装 com ...