HDU 2222 Keywords Search(AC自己主动机模板题)
题意:给出一个字符串和若干个模板,求出在文本串中出现的模板个数。
思路:由于有可能有反复的模板,trie树权值记录每一个模板出现的次数就可以。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<map>
#include<set>
#include<ctime>
#define eps 1e-6
#define LL long long
#define pii (pair<int, int>)
//#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std; const int maxn = 1000000 + 100;
const int SIGMA_SIZE = 26;
const int maxnode = 1000000+100;
int n, ans;
bool vis[maxn];
map<string, int> ms;
int ch[maxnode][SIGMA_SIZE+5];
int val[maxnode];
int idx(char c) {return c - 'a';}
struct Trie {
int sz;
Trie() { sz = 1; memset(ch[0], 0, sizeof(ch[0])); memset(vis, 0, sizeof(vis)); }
void insert(char *s) {
int u = 0, n = strlen(s);
for(int i = 0; i < n; i++) {
int c = idx(s[i]);
if(!ch[u][c]) {
memset(ch[sz], 0, sizeof(ch[sz]));
val[sz] = 0;
ch[u][c] = sz++;
}
u = ch[u][c];
}
val[u]++;
}
}; //ac自己主动机
int last[maxn], f[maxn];
void print(int j) {
if(j && !vis[j]) {
ans += val[j]; vis[j] = 1;
print(last[j]);
}
} int getFail() {
queue<int> q;
f[0] = 0;
for(int c = 0; c < SIGMA_SIZE; c++) {
int u = ch[0][c];
if(u) {
f[u] = 0; q.push(u); last[u] = 0;
}
}
while(!q.empty()) {
int r = q.front(); q.pop();
for(int c = 0; c < SIGMA_SIZE; c++) {
int u = ch[r][c];
if(!u) {
ch[r][c] = ch[f[r]][c];
continue;
}
q.push(u);
int v = f[r];
while(v && !ch[v][c]) v = f[v];
f[u] = ch[v][c];
last[u] = val[f[u]] ? f[u] : last[f[u]];
}
}
} void find_T(char* T) {
int n = strlen(T);
int j = 0;
for(int i = 0; i < n; i++) {
int c = idx(T[i]);
j = ch[j][c];
if(val[j]) print(j);
else if(last[j]) print(last[j]);
}
} char tmp[105];
char text[1000000+1000];
int main() {
//freopen("input.txt", "r", stdin);
int T; cin >> T;
while(T--) {
scanf("%d", &n);
Trie trie;
ans = 0;
for(int i = 0; i < n; i++) {
scanf("%s", tmp);
trie.insert(tmp);
}
getFail();
scanf("%s", text);
find_T(text);
cout << ans << endl;
}
return 0;
}
HDU 2222 Keywords Search(AC自己主动机模板题)的更多相关文章
- HDU 2222 Keywords Search AC自己主动机入门题
单词统计的题目,给出一些单词,统计有多少单词在一个文本中出现,最经典的入门题了. AC自己主动机的基础: 1 Trie. 以这个数据结构为基础的,只是添加一个fail指针和构造fail的函数 2 KM ...
- hdu 2222 Keywords Search ac自己主动机
点击打开链接题目链接 Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Ja ...
- HDU 5384 Danganronpa (AC自己主动机模板题)
题意:给出n个文本和m个模板.求每一个文本中全部模板出现的总次数. 思路:Trie树权值记录每一个模板的个数.对于每一个文本跑一边find就可以. #include<cstdio> #in ...
- hdu5384 AC自己主动机模板题,统计模式串在给定串中出现的个数
http://acm.hdu.edu.cn/showproblem.php?pid=5384 Problem Description Danganronpa is a video game franc ...
- NYOJ 1085 数单词 (AC自己主动机模板题)
数单词 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描写叙述 为了可以顺利通过英语四六级考试,如今大家每天早上都会早起读英语. LYH本来以为自己在6月份的考试中能够通过六 ...
- hdu2222--Keywords Search+AC自己主动机模板
题目链接:pid=2222">点击进入 KMP对模式串进行处理.然后就能够方便的推断模式串是否在目标串中出现了:这显示适合一个模式串多个目标串的情况.可是假设模式串有多个,这时假设还用 ...
- 【HDU】病毒侵袭(AC自己主动机模板题)
AC自己主动机的模板题.因为输入的字符串中的字符不保证全为小写字母.所以范围应该在130之前,而前31位字符是不可能出如今字符串的(不懂得查下ACSII表即可了).所以仅仅须要开的结点数组大小为130 ...
- hdu 2222 Keywords Search——AC自动机
题目:http://acm.hdu.edu.cn/showproblem.php?pid=2222 第一道AC自动机! T了无数边后终于知道原来它是把若干询问串建一个自动机,把模式串放在上面跑:而且只 ...
- hdu 2222 Keywords Search ac自动机入门
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:有N(N <= 10000)个长度不超过50的模式串和一个长度不超过1e6的文本串. ...
随机推荐
- OpenRail中地形模型特征的含义
点或点高程 点(附有 X.Y.Z 数据)与任何其他点之间没有功能关系.对开阔地形的随机测量拍摄可以被当做随机点的例子.点图元,如单元.圆圈和文字串是典型的 MICROSTATION 图元,用于用图表定 ...
- [Hyperapp] Render Text with JSX in Hyperapp
Hyperapp is an ultra lightweight (1kb), minimal, functional, JavaScript library for building UIs. It ...
- Linux以下的两种文件锁
文件锁是一种文件读写机制.在不论什么特定的时间仅仅同意一个进程訪问一个文件. 利用这样的机制可以使读写单个文件的过程变得更安全. 在这篇文章中.我们将探讨Linux中不同类型的文件锁,并通过演示样例程 ...
- UVALive 4223 / HDU 2962 spfa + 二分
Trucking Problem Description A certain local trucking company would like to transport some goods on ...
- bzoj5106: [CodePlus2017]汀博尔(二分答案)
5106: [CodePlus2017]汀博尔 题目:传送门 题解: 百题纪念!!! 原谅一下第一百题刷了到水题... 直接二分月份然后判断(注意上界大小) 代码: #include<cstdi ...
- 火狐访问IIS出现404,而Chrome可以正常访问
需要在web.config中的handlers中添加如下节点,保存之后,需要重启电脑. <remove name="ExtensionlessUrlHandler-Integrated ...
- hdoj--5500--Reorder the Books(技巧)
Reorder the Books Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Othe ...
- 新疆大学OJ(ACM) 1047: string 字符串排序
1047: string 时间限制: 1 Sec 内存限制: 128 MB 题目描述 有n个字符串字符串n<=50000,把所有字符串串起来,得到一个字典序最小的字符串. 输入 输入第一行是一 ...
- HDU 1338 Game Prediction【贪心】
解题思路: 给出 n m 牌的号码是从1到n*m 你手里的牌的号码是1到n*m之间的任意n个数,每张牌都只有一张,问你至少赢多少次 可以转化为你最多输max次,那么至少赢n-max次 而最多输max ...
- 3dmax实例教程-使用3ds Max 创建一个完整的场景
本篇教程讲述了利用3ds max创建一个完整的场景. 灵感来源:当我在遇到一些事情睡不着觉的时候我便在努力想象一些别的事情,于是我便想到了这个场景,其实对于我的这个角色我即没有参考图也没有草稿图,有的 ...