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. Entity Framework Tutorial Basics(6):Model Browser

    Model Browser: We have created our first Entity Data Model for School database in the previous secti ...

  2. 同一个id出现多条数据的问题

    这是disial出现的一个bug,花了近两天时间才解决,原因,要在dto的类前加上注解,让它延迟加载. -----后期补充.结合代码.

  3. FileSaver.js导出json文件和文本

    最近刚刚写了个json数据导出生成Excel文件的,顺便总结下利用FileSaver.js导出其他文件的,这里要注意的一个点就是,当导出的是json文件或是txt文件时,导出的内容要是字符串,特别当时 ...

  4. SQLyog使用,连接ubuntu虚拟机(Error No.2003)

    1.为mysql设置远程访问权限 mysql> grant all PRIVILEGES on *.* to ‘账号’@’%’ identified by ‘密码′; mysql> flu ...

  5. “网易大数据讲堂第一期:数说”直播活动资料:课程回放收看及PPT下载

    欢迎访问网易云社区,了解更多网易技术产品运营经验. "网易大数据讲堂第一期:数说"直播活动昨晚顺利举行.感谢各位"数"友的支持和参与. 本次活动PPT可点击这里 ...

  6. JavaScript(3)——Object-Oriented Design

    自己定义函数 var Winston = function(nickname, age, x, y) { this.nickname = nickname; this.age = age + &quo ...

  7. windwos下安装nginx(转)

    1.windows下安装Nginx1.1 从nginx官网下载相应的安装包:http://nginx.org/  1.2 建议下载  下载稳定版 1.3 解压到相应的目录,比如我是e盘 然后修改目录名 ...

  8. PHP里public和private的区别

    public  公共的,谁都可以用 private 私有的,当前class可以随便用,外部不能调用

  9. LAMP实战之构建博客网站

    1.首先检查LAMP环境 [root@cairui htdocs]# ps -ef | grep httpd php Mar03 ? :: /opt/apache2.2.34/bin/httpd -k ...

  10. P5056 【模板】插头dp

    \(\color{#0066ff}{ 题目描述 }\) 给出n*m的方格,有些格子不能铺线,其它格子必须铺,形成一个闭合回路.问有多少种铺法? \(\color{#0066ff}{输入格式}\) 第1 ...