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 ...
随机推荐
- OnActionExecuting验证用户登录
代码 using Common; using Service; using Service.IService; using System; using System.Collections.Gener ...
- Vue 1.0 和 2.0 执行顺序
// Vue 生命周期 // Vue 1.0 // 执行步骤:选项/生命周期钩子 // 1 init // 2 created // 3 beforeCompile // 4 compiled // ...
- Java并发-volatile的原理及用法
Java并发-volatile的原理及用法 volatile属性:可见性.保证有序性.不保证原子性.一.volatile可见性 在Java的内存中所有的变量都存在主内存中,每个线程有单独CPU缓存内存 ...
- nginx 的 负载均衡
一.正向代理和反向代理 1.正向代理 正向代理类似一个跳板机,代理访问外部资源. 正向代理是客户端和目标服务器之间的代理服务器(中间服务器).为了从指定的服务器取得内容,客户端向代理服务器发送一个请求 ...
- Taxi
/* After the lessons n groups of schoolchildren went outside and decided to visit Polycarpus to cele ...
- Android.FamousBlogs
1. cyrilmottier http://cyrilmottier.com/ http://cyrilmottier.com/about/ 2. greenrobot http://greenro ...
- 标准IO缓冲机制
参考资料: https://q16964777.iteye.com/blog/2228244 知道缓冲有几种模式:无缓冲.行缓冲.全缓冲.通过判断FILTE中的 _flags 的判断可以知道究竟是那种 ...
- php7.0-fpm.sock
.sock 这种方式是套接字的方式连接的
- Netty Reator(二)Scalable IO in Java
Netty Reator(二)Scalable IO in Java Netty 系列目录 (https://www.cnblogs.com/binarylei/p/10117436.html) Do ...
- maven项目springmvc导包失败-JsonProcessingException
昨天搞了好久还是没有解决,今天把springaop小demo敲一遍时,启动tomcat时报错: org.springframework.beans.factory.BeanCreationExcept ...