题意:

问母串中出现多少个模式串

注意ac自动机的节点总数

#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
inline int Max(int a,int b){return a>b?a:b;}
inline int Min(int a,int b){return a>b?b:a;}
int ANS; #define N 1000010
#define maxnode 250001
#define sigma_size 26 struct Trie{
int ch[maxnode][sigma_size];
int val[maxnode];
int last[maxnode];
int f[maxnode];
int sz;
void init(){
sz=1;
memset(ch,0,sizeof(ch));
memset(val, 0, sizeof(val));
memset(f,0,sizeof(f));
memset(last,0,sizeof(last));
}
int idx(char c){
return c-'a';
}
void print(int j){
if(j){
printf("%d: %d\n", j, val[j]);
print(last[j]);
}
}
void Creat(char *s){
int u = 0, len = strlen(s);
for(int i = 0;i < len;i++){
int c = idx(s[i]);
if(!ch[u][c])
ch[u][c] = sz++; u = ch[u][c];
}
val[u] ++;
}
void getFail(){
queue<int> q;
for(int i = 0; i<sigma_size; i++)
if(ch[0][i]) q.push(ch[0][i]); while(!q.empty()){
int r = q.front(); q.pop();
for(int c = 0; c<sigma_size; c++){
int u = ch[r][c];
if(!u)continue;
q.push(u);
int v = f[r];
while(v && ch[v][c] == 0) v = f[v]; //沿失配边走上去 如果失配后有节点 且 其子节点c存在则结束循环
f[u] = ch[v][c];
last[u] = val[f[u]] ? f[u] : last[f[u]];
}
}
}
void find(char *T){
int len = strlen(T), j = 0;
for(int i = 0; i < len; i++){
int c = idx(T[i]);
while(j && ch[j][c]==0) j = f[j];
j = ch[j][c]; int temp = j;
while(temp && val[temp]){
ANS += val[temp];
val[temp] = 0;
temp = f[temp];
}
}
}
};
Trie ac;
char S1[N];
int Slen; void InputString(){
scanf("%s",S1);
Slen = strlen(S1);
S1[Slen]='\0';
}
int main(){ int T,i,j,n;scanf("%d",&T); while(T--){
ac.init();
scanf("%d",&n);
while(n--){
scanf("%s",S1);
ac.Creat(S1);
}
ac.getFail();
InputString();
ANS = 0;
ac.find(S1); printf("%d\n",ANS); }
return 0;
}
/*
1
5
she
he
say
shr
her
yasherhs ans:
3 */

HDU 2222 AC自动机 裸题的更多相关文章

  1. HDU 2222 AC自动机模板题

    题目: http://acm.hdu.edu.cn/showproblem.php?pid=2222 AC自动机模板题 我现在对AC自动机的理解还一般,就贴一下我参考学习的两篇博客的链接: http: ...

  2. hdu 2222(AC自动机模版题)

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

  3. HDU 2222 AC自动机模版题

    所学的AC自动机都源于斌哥和昀神的想法. 题意:求目标串中出现了几个模式串. 使用一个int型的end数组记录,查询一次. #include <cstdio> #include <c ...

  4. HDU 3065 AC自动机 裸题

    中文题题意不再赘述 注意 失配数组 f  初始化一步到位 #include <stdio.h> #include <string.h> #include <queue&g ...

  5. HDU 2896 AC自动机 裸题

    中文题题意不再赘述 注意字符范围是可见字符,从32开始到95 char c - 32 #include <stdio.h> #include <string.h> #inclu ...

  6. HDU 2222 AC自动机(模版题)

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

  7. Keywords Search HDU - 2222 AC自动机板子题

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

  8. AC自动机裸题

    HDU 2222 Keywords Search 模板题.对模式串建立AC自动机然后在trie树上找一遍目标串即可. # include <cstdio> # include <cs ...

  9. HDU 3065 (AC自动机模板题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3065 题目大意:多个模式串,范围是大写字母.匹配串的字符范围是(0~127).问匹配串中含有哪几种模 ...

随机推荐

  1. Typescript 团队合作的利器

    前言 在介绍Typescript 之前,我需要隆重介绍一个人: 安德斯·海尔斯伯格(Anders Hejlsberg,1960.12~),丹麦人,Turbo Pascal编译器的主要作者,Delphi ...

  2. Xcode断点的一些黑魔法

    转自 只会左键断点?是时候试试这样那样断点了 编码不能没调试,调试不能没断点(Break Point).XCode的断点功能也是越来越强大. 基本断点 如下图,这种是最常用的断点,也是最容易设置.左键 ...

  3. confluence的权限管理

    上一篇解讲如何破解,安装confluence5.8.10,这次主要是看权限管理的实现.因为公司对知识的管理不仅是简单的分享,还要求不同权限的人看到不同的内容,所以在一开始就需要对权限这一块有所了解,以 ...

  4. LAMP的编译日志,

    在CentOS5.2上,编译LAMP的,两年前测试通过的,现在留印 ### 在记事本中 ,不要打开 自动换行,否则一些命令 无法正常运行###把源文件考到/src/目录下,然后进入/src////// ...

  5. Meta元素可视区

    一.网页手机wap2.0网页的head里加入下面这条元标签,在iPhone的浏览器中页面将以原始大小显示,并不允许缩放. <meta name="viewport" cont ...

  6. wechat-php-sdk

    wechat-php-sdk 微信公众平台php版开发包 支持消息加解密方式的明文模式.兼容模式.安全模式 支持自动接入微信公众平台(步骤) 功能模块 Wechat (处理自动接入.获取与回复微信消息 ...

  7. javascript 设为首页 | 加入收藏夹 JS代码

    我们介绍一个可兼容所有浏览器的加入收藏代码代码,大概原理是这样的我们根据获取用户navigator.userAgent.toLowerCase()信息来判断浏览器,根据浏览器是否支持加入收藏js命令, ...

  8. BAE 环境下配置 struts2 + spring + hibernate(SSH)(二)struts2

    在myeclipse下开发的 应用但是 放到BAE下就出现了问题,虽然显示发布成功,但是访问的时候就会出现503 Service Unavailable 错误.通过调整 web.xml 发现纯Serv ...

  9. ios的Ping++支付接入步骤-b

    1. Client 发送支付要素给 Server 2. Server 发送支付请求并将返回的支付凭据传给 Client 3. Client 调起支付控件完成支付 4. 渠道同步返回支付结果给 Clie ...

  10. 最新game

    #include <stdio.h> #include <stdlib.h> #include <string.h> #include <signal.h&g ...