http://acm.split.hdu.edu.cn/showproblem.php?pid=2222

#include <cstdio>
#include <cstdlib>
#include <cstring>
const int maxn = + ;
const int N = ; struct AC {
int son[maxn][N], fail[maxn * N], endPos[maxn * N];
int t, root;
int create() {
++t;
for (int i = ; i < N; ++i) son[t][i] = NULL;
fail[t] = endPos[t] = NULL;
return t;
}
void init() {
t = ;
root = create();
}
int getID(char ch) {
return ch - 'a';
}
void toInsert(char str[]) {
int now = root;
for (int i = ; str[i]; ++i) {
int id = getID(str[i]);
if (son[now][id] == NULL) son[now][id] = create();
now = son[now][id];
}
endPos[now]++;
}
int que[maxn * N];
void buildFail() {
fail[root] = root;
int head = , tail = ;
for (int i = ; i < N; ++i) {
if (son[root][i] == NULL) son[root][i] = root;
else {
fail[son[root][i]] = root;
que[tail++] = son[root][i];
}
}
while (head < tail) {
int cur = que[head++];
for (int i = ; i < N; ++i) {
if (son[cur][i] == NULL) son[cur][i] = son[fail[cur]][i];
else {
fail[son[cur][i]] = son[fail[cur]][i]; //虚拟边已经存在
que[tail++] = son[cur][i];
}
}
}
}
int query(char str[]) {
int now = root, ans = ;
for (int i = ; str[i]; ++i) {
int id = getID(str[i]);
now = son[now][id];
int p = now;
while (p != root && endPos[p] != -) {
ans += endPos[p];
endPos[p] = -;
p = fail[p];
}
}
return ans;
}
} ac; char str[maxn];
void work () {
ac.init();
int n;
scanf("%d", &n);
while (n--) {
scanf("%s", str + );
ac.toInsert(str);
}
scanf("%s", str + );
ac.buildFail();
printf("%d\n", ac.query(str));
return ;
}
int main() {
#ifdef local
freopen("data.txt", "r", stdin);
#endif
int t;
scanf("%d", &t);
while (t--) {
work ();
}
return ;
}

hdu 2222 ac自动机更新模板 for onSite contest的更多相关文章

  1. HDU 2222 AC自动机模板题

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

  2. HDU 2222 (AC自动机)

    HDU 2222 Keywords search Problem : 给若干个模式串,询问目标串中出现了多少个模式串. Solution : 复习了一下AC自动机.需要注意AC自动机中的fail,和n ...

  3. HDU 2222 ----AC自动机

    Problem Description In the modern time, Search engine came into the life of everybody like Google, B ...

  4. HDU 2222 AC自动机 裸题

    题意: 问母串中出现多少个模式串 注意ac自动机的节点总数 #include <stdio.h> #include <string.h> #include <queue& ...

  5. HDU 2222 AC自动机模版题

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

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

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

  7. HDU 2222 & ac自动机模板

    题意: 求n个模板串在匹配串中出现了几个. SOL: 反正就是模板啦...似乎比KMP都简单----这么说似乎有点不道德...毕竟先看的KMP而他们并没有什么不同... 貌似自己的理解和他们画的图还是 ...

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

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

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

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

随机推荐

  1. SDUT 3379 数据结构实验之查找七:线性之哈希表

    数据结构实验之查找七:线性之哈希表 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 根据给定 ...

  2. jemalloc和内存管里

    netty的buffer引入了缓冲池.该缓冲池实现使用了jemalloc的思想.为了看懂这块代码学写了内容分配的知识.这里讲的内存分配是堆的内存分配,其他内容本文不会涉及. 内存分配是面向虚拟内存的而 ...

  3. 笔录---果壳中的C#第一章

    ---恢复内容开始--- 笔录---果壳中的C#第二章 2.1 第一个C#程序 1.C#语句按顺序执行,以“:”结尾. Console.WriteLine();     console 为类,Writ ...

  4. Struts2 引入

    引入:   说:如果一个路径想访问一个类,需求怎么做? 第一种方法,用servlet 第二种方法,用过滤器 第三种方法如下图:把路径和方法都存到map里面,用反射可以执行类下的方法     第三中方法 ...

  5. json序列化.xml序列化.图片转base64.base64转图片.生成缩略图.IEnumerable<TResult> Select<TSource, TResult>做数据转换的五种方式

     JSON序列化 /// <summary> /// JSON序列化 /// </summary> public static class SPDBJsonConvert { ...

  6. MAC的开机启动

    之前了解到MAC下的开机启动和定时自动运行不再沿用cron,而是有自己的launchctl:把启动文件放在/Library/LaunchDaemon/xxx.plist,里面可以记录运行的时间间隔.工 ...

  7. 大白话解说TCP/IP协议三次握手和四次挥手

    背景 和女朋友异地恋一年多,为了保持感情我提议每天晚上视频聊天一次. 从好上开始,到现在,一年多也算坚持下来了. 问题 有时候聊天的过程中,我的网络或者她的网络可能会不好,视频就会卡住,听不到对方的声 ...

  8. hdu4658(广义五边形&分割函数2)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4658 题意:f(x) 为将 x 分成其他数和的形式的方案数.对于 t 组输入,输出 f(xi, k), ...

  9. 洛谷P3413 SAC#1 - 萌数(数位dp)

    题目描述 辣鸡蒟蒻SOL是一个傻逼,他居然觉得数很萌! 好在在他眼里,并不是所有数都是萌的.只有满足“存在长度至少为2的回文子串”的数是萌的——也就是说,101是萌的,因为101本身就是一个回文数:1 ...

  10. Windows自动化---模拟鼠标键盘

    1.PyUserInput(不推荐) python2可以使用PyUserInput库:(不推荐) 支持最基础的鼠标,键盘操作,可以剪贴. 安装的时候:pip install PyUserInput 需 ...