AC自动机 HDOJ 2222 Keywords Search
题意:每个文本串的出现次数
分析:入门题,注意重复的关键字算不同的关键字,还有之前加过的清零。 新模板,加上last跑快一倍
#include <bits/stdc++.h>
struct AC {
static const int NODE = 10000 * 50 + 5;
static const int SIZE = 26;
int ch[NODE][SIZE], fail[NODE], last[NODE];
int end[NODE];
int sz;
void clear() {
memset (ch[0], 0, sizeof (ch[0]));
end[0] = 0;
sz = 1;
}
int idx(char ch) {
return ch - 'a';
}
void insert(char *str) {
int u = 0;
for (int c, i=0; str[i]; ++i) {
c = idx (str[i]);
if (!ch[u][c]) {
memset (ch[sz], 0, sizeof (ch[sz]));
end[sz] = 0;
ch[u][c] = sz++;
}
u = ch[u][c];
}
end[u]++;
}
void build() {
fail[0] = 0;
std::queue<int> que;
for (int c=0; c<SIZE; ++c) {
int u = ch[0][c];
if (u) {
fail[u] = 0;
last[u] = 0;
que.push (u);
}
}
while (!que.empty ()) {
int r = que.front (); que.pop ();
for (int c=0; c<SIZE; ++c) {
int u = ch[r][c];
if (!u) {
ch[r][c] = ch[fail[r]][c];
} else {
fail[u] = ch[fail[r]][c];
last[u] = end[fail[u]] ? fail[u] : last[fail[u]];
que.push (u);
}
}
}
}
int query(char *text) {
int ret = 0, u = 0;
for (int c, i=0; text[i]; ++i) {
c = idx (text[i]);
u = ch[u][c];
int t = u;
while (t) {
ret += end[t];
end[t] = 0;
t = last[t];
}
}
return ret;
}
};
AC ac;
char p[55], t[1000010];
int main(void) { //HDOJ 2222 Keywords Search
int T; scanf ("%d", &T);
while (T--) {
int n; scanf ("%d", &n);
ac.clear ();
for (int i=1; i<=n; ++i) {
scanf ("%s", p); ac.insert (p);
}
ac.build (); scanf ("%s", t);
printf ("%d\n", ac.query (t));
}
return 0;
}
AC自动机 HDOJ 2222 Keywords Search的更多相关文章
- hdoj 2222 Keywords Search(AC自动机)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 思路分析:该问题为多模式匹配问题,使用AC自动机解决:需要注意的问题是如何统计该待查询的字符串包 ...
- hdoj 2222 Keywords Search 【AC自己主动机 入门题】 【求目标串中出现了几个模式串】
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- HDOJ 2222: Keywords Search
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- AC自动机讲解+[HDU2222]:Keywords Search(AC自动机)
首先,有这样一道题: 给你一个单词W和一个文章T,问W在T中出现了几次(原题见POJ3461). OK,so easy~ HASH or KMP 轻松解决. 那么还有一道例题: 给定n个长度不超过50 ...
- 【AC自动机】hdu2222 Keywords Search
AC自动机模板题,给你n个模式串和一个文本串,问你有几个模式串在文本串出现过. 注意防止重复统计 这里推荐一波郭大爷的介绍,简单易懂. http://www.bilibili.com/video/av ...
- HDU 2222 Keywords Search(查询关键字)
HDU 2222 Keywords Search(查询关键字) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K ...
- HDU 2222 Keywords Search(AC自动机模版题)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- HDU 2222 Keywords Search (AC自动机)
题意:给你一些模式串,再给你一串匹配串,问你在匹配串中出现了多少种模式串,模式串可以相同 AC自动机:trie树上进行KMP.首先模式串建立trie树,再求得失配指针(类似next数组),其作用就是在 ...
- hdu 2222 Keywords Search ac自动机入门
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:有N(N <= 10000)个长度不超过50的模式串和一个长度不超过1e6的文本串. ...
随机推荐
- HDU 1588 Gauss Fibonacci(矩阵高速幂+二分等比序列求和)
HDU 1588 Gauss Fibonacci(矩阵高速幂+二分等比序列求和) ACM 题目地址:HDU 1588 Gauss Fibonacci 题意: g(i)=k*i+b;i为变量. 给出 ...
- Meteor软件包管理
Meteor 提供数千种开发应用程序,您可以使用社区包. 添加软件包 您可以查看Meteor官方包服务器: 点击这里. 只搜索你需要的包,并在命令提示符窗口中添加它. 例如,想使用 http 包添加到 ...
- 深入理解 C 指针阅读笔记 -- 第六章
Chapter6.h #ifndef __CHAPTER_6_ #define __CHAPTER_6_ /*<深入理解C指针>学习笔记 -- 第六章*/ typedef struct _ ...
- ASP.NET Web Pages - 教程
ASP.NET Web Pages - 教程 ASP.NET 是一个使用 HTML.CSS.JavaScript 和服务器脚本创建网页和网站的开发框架. ASP.NET 支持三种不同的开发模式:Web ...
- 【剑指Offer面试题】 九度OJ1517:链表中倒数第k个结点
鲁棒性是指程序可以推断输入是否符合规范要求,并对不和要求的输入予以 合理的处理. 题目链接地址: http://ac.jobdu.com/problem.php?pid=1517 题目1517:链表中 ...
- CocoaPoda在iOS开发中的使用
CocoaPoda在iOS开发中的使用 CocoaPods 简介 CocoaPods是iOS开发中不可避免的依赖管理第三方的工具,能简化一些第三方库文件需要添加编译参数及依赖库的繁复工作 CocoaP ...
- Installation error: INSTALL_FAILED_CPU_ABI_INCOMPATIBLE
解决方法: http://my.oschina.net/u/242764/blog/375909 当我们安装好Genymotion后,把Android运用部署到上面调试时,console 控制台会报错 ...
- Django值中间件
1,还是那句话:写代码的逻辑遵循:简洁,重复性高,可维护性高 1.1>中间件:中间件是一种用来处理Django的请求和响应的框架级别的钩子.它是一个轻量,低级别的插件系统,用于在全局范围内改变D ...
- Educational Codeforces Round 18 C. Divide by Three DP
C. Divide by Three A positive integer number n is written on a blackboard. It consists of not more ...
- mysql 系统函数
SELECT VERSION() -- 获取 mysql版本号 SELECT CONNECTION_ID() -- 查看服务启动后 用户的连接次数 SELECT DATABASE(),SCHEMA() ...