题:http://acm.hdu.edu.cn/showproblem.php?pid=2222

学习出:https://bestsort.cn/2019/04/28/402/

主要是fail的建立。在跳的过程就是不断跳fail,而不是跳到一个fail再往下!!

#include<iostream>
#include<queue>
#include<algorithm>
#include<cstring>
#include<string>
using namespace std;
typedef long long ll;
const int M=2e6+; int trie[M][]; //字典树
int cntword[M]; //记录该单词出现次数
int fail[M]; //失败时的回溯指针
int cnt=; void insert(string s){
int root = ;
for(int i=;i<s.size();i++){
int next = s[i] - 'a';
if(!trie[root][next])
trie[root][next] = ++cnt;
root = trie[root][next];
}
cntword[root]++; //当前节点单词数+1
}
void getfail(){
queue <int>q;
for(int i=;i<;i++){ //将第二层所有出现了的字母扔进队列
if(trie[][i]){
fail[trie[][i]] = ;
q.push(trie[][i]);
}
} //fail[now] ->当前节点now的失败指针指向的地方
////tire[now][i] -> 下一个字母为i+'a'的节点的下标为tire[now][i]
while(!q.empty()){
int now = q.front();
q.pop(); for(int i=;i<;i++){ //查询26个字母
if(trie[now][i]){
//如果有这个子节点为字母i+'a',则
//让这个节点的失败指针指向(((他父亲节点)的失败指针所指向的那个节点)的下一个节点)
//有点绕,为了方便理解特意加了括号 fail[trie[now][i]] = trie[fail[now]][i];
q.push(trie[now][i]);
}
else//否则就让当前节点的这个子节点
//指向当前节点fail指针的这个子节点
trie[now][i] = trie[fail[now]][i];
}
}
} int query(string s){
int now = ,ans = ;
for(int i=;i<s.size();i++){ //遍历文本串
now = trie[now][s[i]-'a']; //从s[i]点开始寻找
for(int j=now;j && cntword[j]!=-;j=fail[j]){
//一直向下寻找,直到匹配失败(失败指针指向根或者当前节点已找过).
ans += cntword[j];
cntword[j] = -; //将遍历国后的节点标记,防止重复计算
}
}
return ans;
} void Clear(){
for(int i=;i<=cnt;i++){
for(int j=;j<;j++)
trie[i][j]=;
cntword[i]=;
fail[i]=;
} }
int n,t;
int main(){ ios::sync_with_stdio(false);
cin.tie();
cin>>t;
while(t--){
cin>>n;
string s;
for(int i=;i<=n;i++){ cin>>s;
insert(s);
}
fail[]=;
getfail();
cin>>s;
cout<<query(s)<<endl;
Clear();
}
return ;
} /*
5
ash
shex
bcd
sha
sh
ashe 2
*/

hdu2222 (AC自动机模板)的更多相关文章

  1. [hdu2222] [AC自动机模板] Keywords Search [AC自动机]

    AC自动机模板,注意!ch,Fail,lab数组的大小不是n而是节点个数,需要认真计算! #include <iostream> #include <algorithm> #i ...

  2. Keywords Search HDU2222 AC自动机模板题

    ac自动机说起来很复杂,其实和kmp是一样的思路,都是寻找相同前后缀,减少跳的次数.只要理解了kmp是怎么求next数组的,ac自动机bfs甚至比knp还好写. 这里大致说一下kmp求next数组的方 ...

  3. hdu2222(ac自动机模板)

    #include<iostream> #include<cmath> #include<cstdio> #include<cstring> #inclu ...

  4. Hdu 5384 Danganronpa (AC自动机模板)

    题目链接: Hdu 5384 Danganronpa 题目描述: 给出n个目标串Ai,m个模式串Bj,问每个目标串中m个模式串出现的次数总和为多少? 解题思路: 与Hdu 2222  Keywords ...

  5. HDU 2222 AC自动机模板题

    题目: http://acm.hdu.edu.cn/showproblem.php?pid=2222 AC自动机模板题 我现在对AC自动机的理解还一般,就贴一下我参考学习的两篇博客的链接: http: ...

  6. Match:Keywords Search(AC自动机模板)(HDU 2222)

    多模匹配 题目大意:给定很多个字串A,B,C,D,E....,然后再给你目标串str字串,看目标串中出现多少个给定的字串. 经典AC自动机模板题,不多说. #include <iostream& ...

  7. HDU 3065 (AC自动机模板题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3065 题目大意:多个模式串,范围是大写字母.匹配串的字符范围是(0~127).问匹配串中含有哪几种模 ...

  8. HDU 2896 (AC自动机模板题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2896 题目大意:多个模式串.多个匹配串.其中串的字符范围是(0~127).问匹配串中含有哪几个模式串 ...

  9. HDU 2222(AC自动机模板题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2222 题目大意:多个模式串.问匹配串中含有多少个模式串.注意模式串有重复,所以要累计重复结果. 解题 ...

  10. HDU2222 (AC自动机)

    AC自动机模板题. 被卡内存了 死活A不掉.. AC自动机参考教程: http://www.cppblog.com/menjitianya/archive/2014/07/10/207604.html ...

随机推荐

  1. 10 ~ express ~ 使用 cookie 保存用户 信息

    思维导图: (1) 保存 cookie (2)销毁 cookie 一,保存 cookie 1,app.js  . 新增代码 var Cookies = require('cookies') /** * ...

  2. java web实现在线编辑word,并将word导出(二)

    前一篇文章介绍了后台将前台html转为word文档的一种方式,但却有一个问题是没法讲图片放置在生成的word报告中.我在网上找了很多方法,甚至将图片转换成base64编码的方式也不成功.效果如下: 由 ...

  3. HandyJSON.Metadata.Class Xcode10.2, swift5.0 报错 linker command failed with exit code 1

    https://blog.csdn.net/weiwandaixu_/article/details/88842491 2019年03月27日 13:35:40 一如初夏丿 阅读数:31 标签: li ...

  4. 2020/1/30 PHP代码审计之文件上传漏洞

    0x00 漏洞简介 文件上传漏洞是指用户上传了一个可执行的脚本文件,并通过此脚本文件获得了执行服务器端命令的能力.这种攻击是最为直接和有效的,"文件上传"本身是没有问题,有问题的是 ...

  5. UVA - 10118 Free Candies(免费糖果)(dp---记忆化搜索)

    题意:桌上有4堆糖果,每堆有N(N<=40)颗.佳佳有一个最多可以装5颗糖的小篮子.他每次选择一堆糖果,把最顶上的一颗拿到篮子里.如果篮子里有两颗颜色相同的糖果,佳佳就把它们从篮子里拿出来放到自 ...

  6. POJ 3970:Party

    Party Time Limit: 1000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Submit Status ...

  7. map/vector遍历删除

    map遍历删除 map<int, vector<int>>::iterator it = g_map.begin(); for (; it != g_map.end(); /* ...

  8. FPGA CRC-16/XMODEM x16+x12+x5+1

    module crc_16( input clk, input [47:0]mac, input rst, input hash_enable,//哈希控制器使能位 output reg hash_c ...

  9. 吴裕雄--天生自然 JAVASCRIPT开发学习:prototype(原型对象)

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  10. ZJNU 2340/2341/2343 - 罗小黑的“礼物”Ⅰ/Ⅱ/Ⅲ

    把一位数.两位数.三位数……这些所在的范围分开判断 可得1~9这些数范围在[1,9]内 10~99内共有90个数,每个数占两位,所以共有180位在,范围在[10,189]内 同理,100~999内共有 ...