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 ...
随机推荐
- python 面向对象编程 之 反射
1 什么是反射 反射的概念是由Smith在1982年首次提出的,主要是指程序可以访问.检测和修改它本身状态或行为的一种能力(自省).这一概念的提出很快引发了计算机科学领域关于应用反射性的研究.它首先被 ...
- PHP 根据两点的坐标计算之间的距离
define('PI',3.1415926535898); define('EARTH_RADIUS',6378.137); //计算范围,可以做搜索用户 function GetRange($lat ...
- c# 24种设计模式5原型模式(Prototype)
前言 原型模式其实C# Object中已经提供了一个Clone( )方法,平时很少用到,最近读Retrofit源码时候看到有这种使用方式. 定义 原型模式就是在系统clone()标记的基础上,对Clo ...
- Event 事件
事件是建立在委托的基础之上的. http://www.cnblogs.com/lystory/p/5085786.html public class 事件参数 { public 事件参数(string ...
- layer.confirm在ASP.NET控件onclick上面的应用方法
有些时候,你可能要修改控件的事件,元素本身.等,这个时候如何操作呢?下面提供一个思路: <asp:LinkButton Visible="false" ID="sh ...
- 解决 win 7 64 位 vs2010 调试silverlight项目无法加载,提示更新developer ,跟新报 消息 ID: 1517 已安装了 Silverlight 的 64 位版本
出现上面的问题是我们安装的silverlight的版本和系统给的silverlight下载的版本冲突, 解决的方法是,首先卸载Silverlight runtime(也就是默认的silverlight ...
- PAT 甲级 1015 Reversible Primes(20)
1015 Reversible Primes(20 分) A reversible prime in any number system is a prime whose "reverse& ...
- hdu (欧拉函数+容斥原理) GCD
题目链接http://acm.hdu.edu.cn/showproblem.php?pid=1695 看了别人的方法才会做 参考博客http://blog.csdn.net/shiren_Bod/ar ...
- Cookie 和 Session 的区别和联系?session的生命周期?多个服务器部署session的管理?
一.session 和 cookie 1.cookie Cookie会根据响应报文里的一个叫做Set-Cookie的首部字段信息,通知客户端保存Cookie.当下次客户端再向服务端发起请求时,客户端会 ...
- http协议(一)一些基础知识
当我们在浏览器的地址栏中输入网址,然后点击回车,接着,浏览器就会呈现出我们需要的web界面,那么,这个界面是怎么产生的? web的界面是根据我们输入的URL(网址.地址),浏览器从服务器端获取对应的文 ...