hdu2222(ac自动机模板)
#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn=*+;
struct node{
int son[],f,v,cnt;
void rnw(){memset(son,,sizeof(son));f=v=cnt=;}
}ch[maxn];
char s[],t[];
int tot,q[maxn],head,tail,T,n;
void add(char s[]){
int u=,len;
len=strlen(s);
for(int i=;i<len;++i){
int c=s[i]-'a';
if(!ch[u].son[c]){ch[u].son[c]=++tot;}
u=ch[u].son[c];
}
ch[u].cnt++;
}
void pre(){
head=tail=;
ch[].f=;
for(int i=;i<;++i){
int u=ch[].son[i];
if(u){
ch[u].f=;
q[++tail]=u;
}
}
while(head<tail){
int now=q[++head];
for(int i=;i<;++i){
int u=ch[now].son[i];
if(!u){
ch[now].son[i]=ch[ch[now].f].son[i];
continue;
}
q[++tail]=u;
int v=ch[now].f;
while(v&&!ch[v].son[i])v=ch[v].f;
ch[u].f=ch[v].son[i];
}
}
}
ll solve(char t[]){
ll res=;
int len,u=;
len=strlen(t);
for(int i=;i<len;++i){
int c=t[i]-'a';
u=ch[u].son[c];
//if(ch[u].cnt){
int tmp=u;
while(tmp){
if(ch[tmp].cnt>=){
res+=ch[tmp].cnt;
ch[tmp].cnt=-;
}
else{break;}
tmp=ch[tmp].f;
}
//}
}
return res;
}
int main(){
cin>>T;
while(T--){
tot=;
for(int i=;i<=;++i)ch[i].rnw();
scanf("%d",&n);
for(int i=;i<=n;++i){
scanf("%s",s);add(s);
}
scanf("%s",t);
pre();
printf("%lld\n",solve(t));
}
system("pause");
return ;
}
/*
1
2
abcdefgh
efg
abcdefgh
*/
PS:那个注释掉的if不能加,这个串就算不是一个结尾也要往前跳,hack数据就写在下面。
hdu2222(ac自动机模板)的更多相关文章
- [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数组的方 ...
- Hdu 5384 Danganronpa (AC自动机模板)
题目链接: Hdu 5384 Danganronpa 题目描述: 给出n个目标串Ai,m个模式串Bj,问每个目标串中m个模式串出现的次数总和为多少? 解题思路: 与Hdu 2222 Keywords ...
- HDU 2222 AC自动机模板题
题目: http://acm.hdu.edu.cn/showproblem.php?pid=2222 AC自动机模板题 我现在对AC自动机的理解还一般,就贴一下我参考学习的两篇博客的链接: http: ...
- Match:Keywords Search(AC自动机模板)(HDU 2222)
多模匹配 题目大意:给定很多个字串A,B,C,D,E....,然后再给你目标串str字串,看目标串中出现多少个给定的字串. 经典AC自动机模板题,不多说. #include <iostream& ...
- HDU 3065 (AC自动机模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3065 题目大意:多个模式串,范围是大写字母.匹配串的字符范围是(0~127).问匹配串中含有哪几种模 ...
- HDU 2896 (AC自动机模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2896 题目大意:多个模式串.多个匹配串.其中串的字符范围是(0~127).问匹配串中含有哪几个模式串 ...
- HDU 2222(AC自动机模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2222 题目大意:多个模式串.问匹配串中含有多少个模式串.注意模式串有重复,所以要累计重复结果. 解题 ...
- HDU2222 (AC自动机)
AC自动机模板题. 被卡内存了 死活A不掉.. AC自动机参考教程: http://www.cppblog.com/menjitianya/archive/2014/07/10/207604.html ...
随机推荐
- Jmeter常用脚本开发之Beanshell Sampler
Beanshell Sampler Beanshell介绍:是一种完全符合java语法规范的脚本语言,且又拥有自己的一些语法和方法:是一种松散类型的脚本语言:它执行标准java语句和表达式,另外它还包 ...
- (转)EasyUI 分页总结
最近用到了easyui的分页和搜索栏功能,使用过程中由于运用不熟练导致其间也出现过一些问题,下面做个小结,供大家共同学习.1.首先使用EasyUI 的DataGrid分页,得加载其js类库:< ...
- PHP 异步使用swoole的可行性测试
PHP的异步.并行.高性能网络通信引擎swoole框架,在一开始我就比较注意,原因无他,php在swoole未出的情况下在多线程调度上确实算得上没有较好的解决方案. 我以系统的注册流程举例,在比较复杂 ...
- centos vncviewer
CentOS6.5 安装vncserver实现图形化访问 一. 安装gnome图形化桌面 #yum groupinstall -y "X Window System" #yum ...
- Contest with Drinks Easy
/* Problem Statement Joisino is about to compete in the final round of a certain programming competi ...
- php mysql 丢失更新
php mysql 丢失更新问题,搜索整个互联网,很少有讲到,也许和php程序员出身一般都是非科班出身有关系吧. 另外php程序一般都是简单数据,很少有并发一致性问题,所以大家都没有谁专门提出这个问题 ...
- libpcap 库使用(一)
参考资料: http://www.tcpdump.org/ DESCRIPTION The Packet Capture library provides a high level interface ...
- Netty 源码(二)NioEventLoop 之 Channel 注册
Netty 源码(二)NioEventLoop 之 Channel 注册 Netty 系列目录(https://www.cnblogs.com/binarylei/p/10117436.html) 一 ...
- Maximum Average Subarray I LT643
Given an array consisting of n integers, find the contiguous subarray of given length k that has the ...
- java多线程知识点
下面是我学习多线程记录的知识点,并没详细讲解每个知识点,只是将重要的知识点记录下来,有时间可以看看,如果有不对的地方,欢迎大家指出,谢谢! 1.多线程的状态和创建方式: 线程的状态: ...