AC自动机 hdu2222
#include <iostream>
using namespace std; struct Node{
Node *next[];
Node* fail;
int count;
Node(){
for (int i = ; i < ; i++){
next[i] = NULL;
}
fail = NULL;
count = ;
}
};
char words[], s[];
Node* d[];
void insert(char words[], Node *root){
int i, len = strlen(words), v;
Node *p = root;
for (i = ; i < len; i++){
v = words[i] - 'a';
if (p->next[v] == NULL){
p->next[v] = new Node();
}
p = p->next[v];
}
p->count++;
}
void build(Node *root){
int head, tail, i;
Node *p, *temp;
head = ;
tail = ;
root->fail = NULL;
d[head] = root;
while (head <= tail){
temp = d[head++];
for (i = ; i < ; i++){
if (temp->next[i] == NULL) continue;
if (temp == root){
temp->next[i]->fail = root;
}
else{
p = temp->fail;
while (p != NULL){
if (p->next[i] != NULL){
temp->next[i]->fail = p->next[i];
break;
}
p = p->fail;
}
if (p == NULL){
temp->next[i]->fail = root;
}
}
d[++tail] = temp->next[i];
}
}
} int query(char s[], Node* root){
int ans = , len = strlen(s), i, v;
Node *p = root, *temp;
for (i = ; i < len; i++){
v = s[i] - 'a';
while (p->next[v] == NULL && p != root){
p = p->fail;
}
p = (p->next[v] != NULL) ? p->next[v] : root;
temp = p;
while (temp != root&&temp->count != -){
ans += temp->count;
temp->count = -;
temp = temp->fail;
}
}
return ans;
} int main(){
int m, n, i;
Node *root;
cin >> m;
while (m--){
root = new Node();
cin >> n;
for (i = ; i < n; i++){
cin >> words;
insert(words, root);
}
build(root);
cin >> s;
cout << query(s, root) << endl;
}
}
AC自动机 hdu2222的更多相关文章
- 【HDU2222】Keywords Search AC自动机
[HDU2222]Keywords Search Problem Description In the modern time, Search engine came into the life of ...
- 【HDU2222】Keywords Search(AC自动机)
Problem Description In the modern time, Search engine came into the life of everybody like Google, B ...
- HDU2222 Keywords Search 【AC自动机】
HDU2222 Keywords Search Problem Description In the modern time, Search engine came into the life of ...
- 【AC自动机】hdu2222 Keywords Search
AC自动机模板题,给你n个模式串和一个文本串,问你有几个模式串在文本串出现过. 注意防止重复统计 这里推荐一波郭大爷的介绍,简单易懂. http://www.bilibili.com/video/av ...
- HDU-2222 Keywords Search 字符串问题 AC自动机
题目链接:https://cn.vjudge.net/problem/HDU-2222 题意 给一些关键词,和一个待查询的字符串 问这个字符串里包含多少种关键词 思路 AC自动机模版题咯 注意一般情况 ...
- hdu2222 Keywords Search ac自动机
地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=2222 题目: Keywords Search Time Limit: 2000/1000 MS ...
- HDU-2222 Keywords Search(AC自动机--模板题)
题目大意:统计一共出现了多少次模板串. 题目分析:AC自动机的模板题.不过这题有坑,相同的模板串不能只算一次. 代码如下: # include<iostream> # include< ...
- HDU2222 Keywords Search [AC自动机模板]
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- HDU2222 Keywords Search(AC自动机模板)
AC自动机是一种多模式匹配的算法.大概过程如下: 首先所有模式串构造一棵Trie树,Trie树上的每个非根结点都代表一个从根出发到该点路径的字符串. 然后每个结点都计算出其fail指针的值,这个fai ...
随机推荐
- codeforces 540 B School Marks【贪心】
题意:一共n个数,给出其中k个数,要求这n个数的中位数为y,这n个数的和不超过x,补全剩下的n-k个数 先统计给出的k个数里面比中位数小的数, 如果cnt<=n/2,说明中位数还没有出现,把这n ...
- hp服务器安装exsi5.5
启动按f8进入raid制造页面: 1. 插入exsi5.5光盘 2. 按下开机键(开机比较慢需要等待一段时间) 3. 进入启动项目界面(插入光盘后启动会让你选择启动项.选择1光盘启动) 接下来按 ...
- Embedding Flash Fullscreen in the Browser Window
For Developers > Design Documents > Embedding Flash Fullscreen in the Browser Window Auth ...
- php八大设计模式之装饰器模式
我们都知道,得到一匹布需要大概这么几步: 1.染色 2.印花 3.裁剪 这种形式在面向对象中怎么实现呢? 面向过程[继承模式]实现: 继承模式得到需要的布料,一步一步的加工. 继承的层次越来越深,扩展 ...
- iOS开发——NSString小结
1.创建常量字符串. NSString *astring = @"This is a String!"; 2.创建空字符串,给予赋值. NSString *astring = ...
- /*+parallel(t,4)*/在SQL调优中的重要作用!
谈谈HINT /*+parallel(t,4)*/在SQL调优中的重要作用! /*+parallel(t,4)*/在大表查询等操作中能够起到良好的效果,基于并行查询要启动并行进程.分配任务与系统资源. ...
- ArcGIS api for javascript——查找任务-在地图上查找要素
描述 本例展示了如何使用查找任务搜索数据.本例在地图上用图表显示结果并用DojoX的grid格式化结果为表格样式. FindTask构造函数需要一个ArcGIS Server地图服务的URL.本例使用 ...
- C语言函数--E
函数名: ecvt 功 能: 把一个浮点数转换为字符串 用 法: char ecvt(double value, int ndigit, int *decpt, int *sign); 程序例: #i ...
- 分别改动Cube每一个面的贴图UV(Unity3D开发之十八)
猴子原创.欢迎转载.转载请注明: 转载自Cocos2Der-CSDN,谢谢! 原文地址: http://blog.csdn.net/cocos2der/article/details/46611169 ...
- Android——4.2 - 3G移植之路之 APN (五)
APN,这东西对于刚接触的人来说并非那么好理解.对于3G移植上网不可缺少,这里记录一下. 撰写不易,转载请注明出处:http://blog.csdn.net/jscese/article/detail ...