#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的更多相关文章

  1. 【HDU2222】Keywords Search AC自动机

    [HDU2222]Keywords Search Problem Description In the modern time, Search engine came into the life of ...

  2. 【HDU2222】Keywords Search(AC自动机)

    Problem Description In the modern time, Search engine came into the life of everybody like Google, B ...

  3. HDU2222 Keywords Search 【AC自动机】

    HDU2222 Keywords Search Problem Description In the modern time, Search engine came into the life of ...

  4. 【AC自动机】hdu2222 Keywords Search

    AC自动机模板题,给你n个模式串和一个文本串,问你有几个模式串在文本串出现过. 注意防止重复统计 这里推荐一波郭大爷的介绍,简单易懂. http://www.bilibili.com/video/av ...

  5. HDU-2222 Keywords Search 字符串问题 AC自动机

    题目链接:https://cn.vjudge.net/problem/HDU-2222 题意 给一些关键词,和一个待查询的字符串 问这个字符串里包含多少种关键词 思路 AC自动机模版题咯 注意一般情况 ...

  6. hdu2222 Keywords Search ac自动机

    地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=2222 题目: Keywords Search Time Limit: 2000/1000 MS ...

  7. HDU-2222 Keywords Search(AC自动机--模板题)

    题目大意:统计一共出现了多少次模板串. 题目分析:AC自动机的模板题.不过这题有坑,相同的模板串不能只算一次. 代码如下: # include<iostream> # include< ...

  8. HDU2222 Keywords Search [AC自动机模板]

    Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others ...

  9. HDU2222 Keywords Search(AC自动机模板)

    AC自动机是一种多模式匹配的算法.大概过程如下: 首先所有模式串构造一棵Trie树,Trie树上的每个非根结点都代表一个从根出发到该点路径的字符串. 然后每个结点都计算出其fail指针的值,这个fai ...

随机推荐

  1. 洛谷 P1368 工艺 后缀自动机 求最小表示

    后缀自动机沙茶题 将字符串复制一次,建立后缀自动机. 在后缀自动机上贪心走 $n$ 次即可. Code: #include <cstdio> #include <algorithm& ...

  2. MFC的学习路线

    首先,MFC是比较难的!比C#和VB要难得多.MFC是基于C++的.首先C++必须熟悉.MFC主要是学习里面的控件的使用. 建议学习路线: 1. 易语言中文编程从入门到精通: https://deta ...

  3. CF981C(菊花图)

    题目描述 RAMESS知道很多关于树的问题(无循环的无向连通图)! 他创建了一个新的有用的树的划分,但他不知道如何构造它,所以他请求你的帮助! 划分是从树上的边中分裂出一些简单的路径,使得每个两条路径 ...

  4. Network authentication method and device for implementing the same

    A network authentication method is to be implemented using a network authentication device and a use ...

  5. Mac上配置 Ruby on Rails和Git

    Ruby on Rails on Mac =============================================================================== ...

  6. Effective C++ 条款13

    以对象管理资源 资源的种类非常多,动态分配的内存.文件描写叙述器.相互排斥锁.图像界面中画刷.数据库连接.网络socket等. 资源通常是有限的.当你不用时,必须释放.不然就会造成资源浪费.更严重的情 ...

  7. 学password学一定得学程序

    题目描写叙述 以前.ZYJ同学非常喜欢password学.有一天,他发现了一个非常长非常长的字符串S1.他非常好奇那代表着什么,于是奇妙的WL给了他还有一个字符串S2.可是非常不幸的是,WL忘记跟他说 ...

  8. 识别CentOS和Ubuntu的系统版本

    识别CentOS和Ubuntu的系统版本1.用 lsb-release#!/bin/bashInstall_LSB(){        if [ "$PM" = "yum ...

  9. 20.发送http请求服务 ($http)

    转自:https://www.cnblogs.com/best/tag/Angular/ 服务从代码直接与服务器进行交互,底层是通过实现,与中http服务从AngularJS代码直接与Web服务器进行 ...

  10. codeforces Gym100589H Count Subarrays 树状数组/线段树+离散化

    题意:给你一个数组,问你有多少子数组中的逆元数不小于K个,N<105 还在研究中