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. [转]delphi xe6 android屏幕操持常亮

    1) setting the Project Options, Uses Permissions, Wake lock = True 2) Adding Android.JNI.PowerManage ...

  2. Java泛型读书笔记 (二)

    关于Java泛型擦除后,继承一个泛型类带来的问题 有如下泛型类Pair: public class Pair<T> { private T second; private T first; ...

  3. 封闭解(Closed-form solution)、解析解(Analytical solution)、数值解(Numerical solution) 释义

    转俞夕的博客 (侵删) 1 解析解 解析解(Analytical solution) 就是根据严格的公式推导,给出任意的自变量就可以求出其因变量,也就是问题的解,然后可以利用这些公式计算相应的问题.所 ...

  4. Python中dataframe\ array\ list相互转化

    import pandas as pd import numpy as np #创建列表 a1=[1,2,3] #arange函数:指定初始值.终值.步长来创建数组 a2=np.arange(0,1, ...

  5. Windows10远程报错:由于CredSSP加密Oracle修正(ps:Win10家庭版)

    Windows10远程桌面连接 报错信息 : 网上找到方法 但是奈何是 "Win10家庭版" 不能使用这个办法,具体操作可以看最后的引用链接 !!!! 策略路径:“计算机配置”-& ...

  6. Linux基础学习(二)

    前言: 我们在上一节了解了一下linux的硬件组成,虽然也许对具体的东西还不甚了解,但是我们知道了linux下一切皆文件这一特性 我们装好了CentOS7的虚拟机(这个可以看别人教程来装起来,比较简单 ...

  7. ThinkPHP3.2.3完整版创建前后台入口文件 http://jingyan.baidu.com/article/7e4409533fc1092fc1e2ef53.html

    ThinkPHP3.2.3完整版创建前后台入口文件   1 2 3 4 5 6 7 分步阅读 ThinkPHP是为了简化企业级应用开发和敏捷WEB应用开发而诞生的优秀的国产php框架,值得我们去探索学 ...

  8. ReentrantReadWriteLock原理

    原文链接:https://www.jianshu.com/p/9f98299a17a5 前言 本篇适用于了解ReentrantLock或ReentrantReadWriteLock的使用,但想要进一步 ...

  9. P2770 航空路线问题

    \(\color{#0066ff}{题目描述}\) 给定一张航空图,图中顶点代表城市,边代表 2 城市间的直通航线.现要求找出一条满足下述限制条件的且途经城市最多的旅行路线. (1)从最西端城市出发, ...

  10. BFS【bzoj1667】: [Usaco2006 Oct]Cows on Skates滑旱冰的奶牛

    1667: [Usaco2006 Oct]Cows on Skates滑旱冰的奶牛 Description 经过跟Farmer John长达数年的谈判,奶牛们终于如愿以偿地得到了想要的旱冰鞋.农场上大 ...