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 ...
随机推荐
- python3.x学习笔记2(基础知识)
1.元组元组其实跟列表差不多,也是存一组数,只是它一旦创建,便不能在修改,所以又叫只读列表语法:names =('shgd','sjdh') 它只有两个方法,一个是count,一个是index 2.字 ...
- 51Nod 独木舟(贪心)
n个人,已知每个人体重.独木舟承重固定,每只独木舟最多坐两个人,可以坐一个人或者两个人.显然要求总重量不超过独木舟承重,假设每个人体重也不超过独木舟承重,问最少需要几只独木舟? Input 第一行包含 ...
- Linux-CentOS5/6启动流程
Linux-CentOS5/6启动流程
- BZOJ 3277/3473 广义后缀自动机
说实话没啥难的. 建一棵广义后缀自动机,暴力自底向上更新即可. 时间复杂度非常玄学,但据说是可以过的. 要注意每个串中相同的子串的贡献是都要加进去的,开始因为这个被坑了好久 QAQ Code: #in ...
- python常用函数库收集。
学习过Python都知道python中有很多库.python本身就是万能胶水,众多强大的库/模块正是它的优势. 收集一些Python常用的函数库,方便大家选择要学习的库,也方便自己学习收集,熟悉运用好 ...
- CMDB学习之六 --客户端请求测试,服务端api优化
客户端使用agent 请求测试,agent使用的POST 请求,使用requests模块 本地采集,汇报服务端 #!/usr/bin/env python # -*- coding:utf-8 -*- ...
- SCU 1095运送物资(最短路)
SCU 1095运送物资(最短路) X国发生了内战.起义军得到了广大人民的支持.在一次战役中,反动军队结集了大量兵力,围攻起义军的主堡W城.为支援前线,后方各个供给基地城市纷纷准备将物资运往W城.各基 ...
- mysql---union的使用方法
union的作用非常easy用来合并两条sql的结果集 语法: SQL1 UNION SQL2 现有一张价格表 要求:求出价格低于2000和价格高于3000的商品.不能用or和not between- ...
- 从C10K到C10M高性能网络的探索与实践
在高性能网络的场景下,C10K是一个具有里程碑意义的场景,15年前它给互联网领域带来了非常大的挑战.发展至今,我们已经进入C10M的场景进行网络性能优化. 这期间有怎样的发展和趋势?环绕着各类指标分别 ...
- Mysql Workbench初体验
可以画图,建立表关系. 分类整理数据表. 可以直接导出sql语句. 可以导出png图片. 可以连接mysql数据库. 基本满足了各项需求. 这次初体验只是基本的功能,这个软件对于mysql还是很牛的.