题意:

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

注意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. 对xml操作

    已知有一个XML文件(bookshop.xml)如下: <?xml version="1.0" encoding="gb2312" ?> <b ...

  2. oracle使用LEFT JOIN关联产生的问题在查询结果中使用CASE WHEN 无法判断

    oracle使用LEFT JOIN关联产生的问题在查询结果中使用CASE WHEN 无法判断 查询方式一: SELECT CASE WHEN (SELECT CAST(SUM(CASE ) THEN ...

  3. Qt Creator编译问题

    有时候需要自己编译Qt Creator,需要注意的就是qmake版本的问题,比如我用4.8.1和4.8.6同样编译出来的Qt Creator在同样的qtconfig-qt4下所呈现的效果是不一样的. ...

  4. call 方法在使用一个指定的this值和若干个指定的参数值的前提下调用某个函数或方法.

    call 方法在使用一个指定的this值和若干个指定的参数值的前提下调用某个函数或方法. 注意:该函数的语法与 apply() 方法的语法几乎完全相同,唯一的区别在于,apply()方法接受的是一个参 ...

  5. 定时器内部的this

    今天写了个例子遇见的: timer1=setTimeout(function(){ $(this).next().css("display","none") } ...

  6. mysql数据类型——枚举enum(‘F’,'M')

    ENUM(“value1”,“value2”,...) 说明:枚举,列值可赋予值列表中的某个成员 允许的属性:除通用属性外无其他属性 缺省值:如果列可为NULL,则为NULL:如果列为NOTNULL, ...

  7. for循环,列表和格式化输出

    一:for 循环 1.          简单的说如果让你输出1到100之间的整数,用while该怎么实现呢? i= : print(i) i+= 看着是不是只有4行,但是有没有更加简单的办法,不妨我 ...

  8. 在MAC下 Python+Django+mysql配置

    今天在搭建Django+mysql环境的时候遇到了一点问题,记录下来. 安装环境:OS X 10.10操作系统,Python 2.7. MySQLdb其实包含在MySQL-python包中,因此无论下 ...

  9. hdu不要62

    Problem Description 杭州人称那些傻乎乎粘嗒嗒的人为62(音:laoer).杭州交通管理局经常会扩充一些的士车牌照,新近出来一个好消息,以后上牌照,不再含有不吉利的数字了,这样一来, ...

  10. 【Java】ArrayList和LinkedList的区别

    一般大家都知道ArrayList和LinkedList的大致区别:      1.ArrayList是实现了基于动态数组的数据结构,LinkedList基于链表的数据结构.      2.对于随机访问 ...