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 ...
随机推荐
- 微信网页JS分享,微信二次分享无缩略图问题
很多时候我们要在微信中分享h5网页,这个时候就得用微信的分享接口来自定义分享的地址.标题.描述.缩略图了. 分享到微信的时候遇到一个问题,就是第一次分享到微信里,是正确的,但是在微信打开分享的链接,再 ...
- POJ3662或洛谷1948 Telephone Lines
二分答案+单源最短路 POJ原题链接 洛谷原题链接 显然可以二分答案,检验\(mid\)可以使用最短路来解决. 将大于\(mid\)的边看成长度为\(1\)的边,说明要使用免费升级服务,否则长度为\( ...
- yii2.0增删改查
//关闭csrf public $enableCsrfValidation = false; 1.sql语句 //查询 $db=\Yii::$app->db ->createCommand ...
- Centos PS1
PS1="[\[\e[35m\]\u\[\e[m\]\[\e[31m\]->\[\e[m\]\[\e[33m\]\H\[\e[m\]\[\e[31m\]->\[\e[m\]\[\ ...
- 企业官网Web原型制作分享-Tesla
Tesla是汽车行业知名的奢华品牌,产品为纯电动汽车,知名度极高.此模板正是取自Tesla的官网,高端大图配上文字排版,彰显了汽车的奢华感觉. 本原型由国产Mockplus(原型工具)和iDoc(智能 ...
- Vue修饰符
为了方便大家写代码,vue.js给大家提供了很多方便的修饰符,比如我们经常用到的取消冒泡,阻止默认事件等等~ 目录 表单修饰符 事件修饰符 鼠标按键修饰符 键值修饰符 v-bind修饰符(实在不知道叫 ...
- Sophus libSophus.so
在编译包含Sophus的源文件的时候,出现如下错误 ../lib/libmyslam.so: undefined reference to `Sophus::SO3::SO3(double, doub ...
- UVA 10405 Longest Common Subsequence
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=16&p ...
- Java学习笔记:23种设计模式
设计模式(Design pattern)的定义: In software engineering, a software design pattern is a general, reusable s ...
- 别人的Linux私房菜(1)计算机概论
计算机主板 早期两个网桥控制通信,北桥连接速度比较快的CPU.内存.显卡.南桥连接较慢的接口,如硬盘,USB,网卡等.北桥的控制器集成到了CPU中. CPU工作频率 外频:CPU与外部组件进行数据传输 ...