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的文本串. ...
随机推荐
- 初探无线安全审计设备WiFi Pineapple Nano系列之PineAP
前言: 之前曾经介绍过国外无线安全审计设备The WiFi Pineapple Nano的SSLsplit模块和ettercap模块及实验. 在玩WiFi Pineapple Nano 设备的过程中, ...
- 外排序 & 败者树 & 多路归并-学习
来来来,根据这篇文章,学一下败者树吧: http://blog.csdn.net/whz_zb/article/details/7425152 一.胜者树 胜者树的一个优点是,如果一个选手的值改变了, ...
- ZOJ 3316 Game 一般图最大匹配带花树
一般图最大匹配带花树: 建图后,计算最大匹配数. 假设有一个联通块不是完美匹配,先手就能够走那个没被匹配到的点.后手不论怎么走,都必定走到一个被匹配的点上.先手就能够顺着这个交错路走下去,最后一定是后 ...
- jquery全选,取消全选
近期项目又用到了这个全选和取消全选的操作. 曾经总是自己写纯JS.如今既然知道怎么写了.那怎样用JQ写得更简洁呢.这样也能学到新的东西.假设乎百度一下果然发现了好东东.感谢OSC的iuhoay. 代码 ...
- EXTJS 4 树形表格组件使用演示样例
EXTJS 4 树形表格组件使用演示样例 一.总体效果图 version=1&modificationDate=1412058826000&api=v2" alt=" ...
- 他人第三方库在linux上的安装
1.下载tar.gz等压缩包 2.解压 3.安装 4.确保路径
- [C#]从URL中获取路径的最简单方法-new Uri(url).AbsolutePath
今天在写代码时遇到这样一个问题: 如何从字符串 "http://job.cnblogs.com/images/job_logo.gif" 中得到 "/images/job ...
- 查看android-support-v4.jar引出的问题
1.前面博文里也写过如何关联android-support-v4.jar的源码 今天新项目用上述方法的时候,竟然不成功..来回反复试了很长时间,最后发现 新建项目,会自动引用一个类库(自动新建的..) ...
- 2016/05/13 thinkphp 3.2.2 ① 数据删除及执行原生sql语句 ②表单验证
[数据删除及执行原生sql语句] delete() 返回受影响的记录条数 $goods -> delete(30); 删除主键值等于30的记录信息 $goods -> delete( ...
- C项目实践--贪吃蛇(2)
12.按键处理 函数名称:key_down 函数功能:按键处理函数,主要包括:1.刚开始或结束时的按键处理,游戏开始时,按任意键进入游戏,游戏运行过程中按回车键是游戏的暂停或开始的切换键:2.游戏运行 ...