题目连接:hdu_2222_Keywords Search

存个自己写的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自动机板子)的更多相关文章

  1. hdu2222 Keywords Search (AC自动机板子

    https://vjudge.net/problem/HDU-2222 题意:给几个模式串和一个文本串,问文本串中包含几个模式串. 思路:贴个板子不解释. #include<cstdio> ...

  2. 【HDU2222】Keywords Search AC自动机

    [HDU2222]Keywords Search Problem Description In the modern time, Search engine came into the life of ...

  3. hdu2222 Keywords Search ac自动机

    地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=2222 题目: Keywords Search Time Limit: 2000/1000 MS ...

  4. HDU2222 Keywords Search [AC自动机模板]

    Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others ...

  5. AC自动机板子题/AC自动机学习笔记!

    想知道484每个萌新oier在最初知道AC自动机的时候都会理解为自动AC稽什么的,,,反正我记得我当初刚知道这个东西的时候,我以为是什么神仙东西,,,(好趴虽然确实是个对菜菜灵巧比较难理解的神仙知识点 ...

  6. Keywords Search(AC自动机模板)

    Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others ...

  7. HDU2222 Keywords Search —— AC自动机

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 Keywords Search Time Limit: 2000/1000 MS (Java/O ...

  8. Keywords Search AC自动机

    In the modern time, Search engine came into the life of everybody like Google, Baidu, etc. Wiskey al ...

  9. Match:Keywords Search(AC自动机模板)(HDU 2222)

    多模匹配 题目大意:给定很多个字串A,B,C,D,E....,然后再给你目标串str字串,看目标串中出现多少个给定的字串. 经典AC自动机模板题,不多说. #include <iostream& ...

随机推荐

  1. 简单的JDBC连接oracle数据库例子

    java连接Oracle数据库 JDBC(Java Data Base Connectivity,java数据库连接),那么我们该如何用java进行数据库的连接呢. import java.sql.C ...

  2. ubuntu server 时区设置问题解决

    1.当执行此命令的时候 ntpdate us.pool.ntp.org 出现一下错误提示 name server cannot be used: Temporary failure in name r ...

  3. ListView间隔设置颜色

    在Adapter的getView中 if (position % 2 == 0) holder.shoucangbt.setBackgroundColor( Color.rgb(246, 246, 2 ...

  4. fragement生命周期

    转自http://www.cnblogs.com/mybkn/ 你的fragment们可以向activity的菜单(按Manu键时出现的东西)添加项,同时也可向动作栏(界面中顶部的那个区域)添加条目, ...

  5. 在 Linux 环境下报错 java.lang.reflect.InvocationTargetException

    今天开发了一个 excel 导出数据的功能,放到 linux 服务器上后发现报错. 捕获到 java.lang.reflect.InvocationTargetException 异常,这个异常不太常 ...

  6. Struts2(result 流 )下载

    jsp: <body> <a href="stream.action?fileName=psb.jpg">psb</a> <br> ...

  7. 开始工作----微信通过get检查当前网站---是否可用

    01开发者--基本配置--url服务器地址---填写好--需要开通微信接口的域名 http://www.cctv.com/xxx.php文件 xxx.php文件 01参考wx_sample.php 0 ...

  8. nefu 899这也是裸的找

    #include <iostream> #include <algorithm> #include <cstdio> using namespace std; in ...

  9. ios 烟花 火焰 雨水 雪花等特效属性

    CAEmitterLayer *snowEmitter = [CAEmitterLayer layer]; //例子发射位置 snowEmitter.emitterPosition = CGPoint ...

  10. caffe的matlab接口一览表

    blob 简述 方法: shape reshape get_diff set_diff 私有方法: check_and_preprocess_shape check_and_preprocess_da ...