hdu_2222_Keywords Search(AC自动机板子)
存个自己写的AC自动机
#include<cstdio>
#include<cstring>
#define F(i,a,b) for(int i=a;i<=b;i++) const int AC_N=*,tyn=;//数量乘串长,类型数量
struct AC_automation{
int tr[AC_N][tyn],cnt[AC_N],Q[AC_N],fail[AC_N],tot;
inline int getid(char x){return x-'a';}
void nw(){cnt[++tot]=;memset(tr[tot],-,sizeof(tr[tot]));}
void init(){tot=-,fail[]=-,nw();}
void insert(char *s,int x=){
for(int len=strlen(s),i=,w;i<len;x=tr[x][w],i++)
if(tr[x][w=getid(s[i])]==-)nw(),tr[x][w]=tot;
cnt[x]++;//串尾标记
}
void build(int head=,int tail=){
for(Q[++tail]=;head<=tail;){
for(int i=,x=Q[head++],p=-;i<tyn;i++)if(~tr[x][i]){
if(x==)fail[tr[][i]]=;
else for(p=fail[x],fail[tr[x][i]]=;~p;p=fail[p])
if(~tr[p][i]){fail[tr[x][i]]=tr[p][i];break;}
Q[++tail]=tr[x][i];
}
}
}
int ask(char *s,int ans=){
for(int len=strlen(s),w,i=,x=,j;i<len;i++){
while(tr[x][w=s[i]-'a']==-&&x)x=fail[x];
x=tr[x][w],x=(~x)?x:,j=x;//加cnt后标记为0,防止重复计数
while(j&&cnt[j])ans+=cnt[j],cnt[j]=,j=fail[j];
}
return ans;
}
}AC;
char buf[];
int main(){
int t,n;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
AC.init();
F(i,,n)scanf("%s",buf),AC.insert(buf);
AC.build();
scanf("%s",buf);
printf("%d\n",AC.ask(buf));
}
return ;
}
hdu_2222_Keywords Search(AC自动机板子)的更多相关文章
- hdu2222 Keywords Search (AC自动机板子
https://vjudge.net/problem/HDU-2222 题意:给几个模式串和一个文本串,问文本串中包含几个模式串. 思路:贴个板子不解释. #include<cstdio> ...
- 【HDU2222】Keywords Search AC自动机
[HDU2222]Keywords Search Problem Description In the modern time, Search engine came into the life of ...
- hdu2222 Keywords Search ac自动机
地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=2222 题目: Keywords Search Time Limit: 2000/1000 MS ...
- HDU2222 Keywords Search [AC自动机模板]
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- AC自动机板子题/AC自动机学习笔记!
想知道484每个萌新oier在最初知道AC自动机的时候都会理解为自动AC稽什么的,,,反正我记得我当初刚知道这个东西的时候,我以为是什么神仙东西,,,(好趴虽然确实是个对菜菜灵巧比较难理解的神仙知识点 ...
- Keywords Search(AC自动机模板)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- HDU2222 Keywords Search —— AC自动机
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 Keywords Search Time Limit: 2000/1000 MS (Java/O ...
- Keywords Search AC自动机
In the modern time, Search engine came into the life of everybody like Google, Baidu, etc. Wiskey al ...
- Match:Keywords Search(AC自动机模板)(HDU 2222)
多模匹配 题目大意:给定很多个字串A,B,C,D,E....,然后再给你目标串str字串,看目标串中出现多少个给定的字串. 经典AC自动机模板题,不多说. #include <iostream& ...
随机推荐
- mysql创建计算列
mysql> create table t(id int auto_increment not null,c1 int,c2 int,c3 int as (c1+c2),primary key( ...
- 模型驱动 ModelDriven
ModelDriven:模型驱动,对所有action的模型对象进行批处理. 我们在开发中, 在action中一般是用实体对象,然后给实体对象get,set方法. RegAction{ User use ...
- C语言指针强制转化的应用
指针类型强制转化在kernel设计中非常常见,这里记录两个非常有意思的用法: 1.对地址进行运算.任何虚拟地址都表示成void *va = (void *) 100, *(va + 1) ==101 ...
- Jekyll: .md to .html with self defined themes..
theme is from here $ gem install jekyll bundler ~ $ jekyll new my-awesome-site ~ $ cd my-awesome-sit ...
- 本篇内容简要介绍BASE64、MD5、SHA、HMAC几种加密算法。
BASE64编码算法不算是真正的加密算法. MD5.SHA.HMAC这三种加密算法,可谓是非可逆加密,就是不可解密的加密方法,我们称之为单向加密算法.我们通常只把他们作为加密的基础.单纯的以上 ...
- 让上下两个DIV块之间有一定距离或没有距离
1.若想上下DIV块之间距离,只需设定:在CSS里设置DIV标签各属性参数为0div{margin:0;border:0;padding:0;}这里就设置了DIV标签CSS属性相当于初始化了DIV标签 ...
- 辨别 ShopEX Ecshop
御剑可以识别ShopEX 或者 Ecshop 特征 ShopEX : 蓝色的icon js里有很多Cookie. <link rel="stylesheet" href=&q ...
- C++ primer 练习 12.7
重做上一题,这次使用shared_ptr 而不是内置指针.上一题题目为:(编写函数,返回一个动态分配的int的vector.将此vector传递给另一个函数,这个函数读取标准输入,将读入的值保存在ve ...
- Git 操作标签的一些命令
如果标签打错了,也是可以删除: $ git tag -d v0.1Deleted tag 'v0.1' (was d96a49b) 如果要推送某个标签到远程,使用git push orign tagn ...
- Spring security 获取当前用户
spring security中当前用户信息 1:如果在jsp页面中获取可以使用spring security的标签库 在页面中引入标签 1 <%@ taglib prefix=" ...