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 ...
随机推荐
- 加载等待loading
自己写的一个小插件,还有很多需要完善... (function ($) { $.fn.StartLoading = function (option) { var defaultV ...
- zookeeper启动闪退
编辑zkServer.cmd在它的尾行加上 pause 就可以将闪退停住来观察闪退的原因. 遇到Exception in thread "main" java.lang. ...
- WLAN RTT (IEEE 802.11mc)
WLAN RTT (IEEE 802.11mc) Android 9 中的 WLAN 往返时间 (RTT) 功能允许设备测量与其他支持设备的距离:无论它们是接入点 (AP) 还是 WLAN 感知对等设 ...
- TrueOS 不再想要成为“桌面 BSD”了
作者: John Paul Wohlscheid 译者: LCTT geekpi | 2018-07-13 22:53 TrueOS 很快会有一些非常重大的变化.今天,我们将了解桌面 BSD 领域将会 ...
- Windows10通过VNC远程连接Ubuntu18.04
1.打开终端输入:sudo apt-get install xrdp vnc4server xbase-clients dconf-editor 2.接着在终端输入: 进入到下面这个界面: 接着按照这 ...
- users---显示当前登录系统的所有用户的用户列表
users命令用于显示当前登录系统的所有用户的用户列表.每个显示的用户名对应一个登录会话.如果一个用户有不止一个登录会话,那他的用户名将显示相同的次数. 语法 users(选项) 选项 --help: ...
- Git学习笔记 2,GitHub常用命令
廖雪峰Git教程 莫烦Git教程 莫烦Git视频教程 文件三个状态,add之后从工作区(原始状态)到暂存区,commit之后从暂存区到版本库 工作区 暂存区 版本库 unstage stage mas ...
- Android如何从外部跳进App
博客出自:http://blog.csdn.net/liuxian13183,转载注明出处! All Rights Reserved ! 这个问题解决了两天时间,因为网上没有完整的解决方案,解决后分享 ...
- javaweb——登陆权限过滤器的编写
http://blog.csdn.net/lzc4869/article/details/50935858
- HTML学习----------DAY1 第二节
使用 Notepad 或 TextEdit 来编写 HTML 可以使用专业的 HTML 编辑器来编辑 HTML: Adobe Dreamweaver Microsoft Expression Web ...