hdu2222 (AC自动机模板)
题: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自动机模板)的更多相关文章
- [hdu2222] [AC自动机模板] Keywords Search [AC自动机]
AC自动机模板,注意!ch,Fail,lab数组的大小不是n而是节点个数,需要认真计算! #include <iostream> #include <algorithm> #i ...
- Keywords Search HDU2222 AC自动机模板题
ac自动机说起来很复杂,其实和kmp是一样的思路,都是寻找相同前后缀,减少跳的次数.只要理解了kmp是怎么求next数组的,ac自动机bfs甚至比knp还好写. 这里大致说一下kmp求next数组的方 ...
- hdu2222(ac自动机模板)
#include<iostream> #include<cmath> #include<cstdio> #include<cstring> #inclu ...
- Hdu 5384 Danganronpa (AC自动机模板)
题目链接: Hdu 5384 Danganronpa 题目描述: 给出n个目标串Ai,m个模式串Bj,问每个目标串中m个模式串出现的次数总和为多少? 解题思路: 与Hdu 2222 Keywords ...
- HDU 2222 AC自动机模板题
题目: http://acm.hdu.edu.cn/showproblem.php?pid=2222 AC自动机模板题 我现在对AC自动机的理解还一般,就贴一下我参考学习的两篇博客的链接: http: ...
- Match:Keywords Search(AC自动机模板)(HDU 2222)
多模匹配 题目大意:给定很多个字串A,B,C,D,E....,然后再给你目标串str字串,看目标串中出现多少个给定的字串. 经典AC自动机模板题,不多说. #include <iostream& ...
- HDU 3065 (AC自动机模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3065 题目大意:多个模式串,范围是大写字母.匹配串的字符范围是(0~127).问匹配串中含有哪几种模 ...
- HDU 2896 (AC自动机模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2896 题目大意:多个模式串.多个匹配串.其中串的字符范围是(0~127).问匹配串中含有哪几个模式串 ...
- HDU 2222(AC自动机模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2222 题目大意:多个模式串.问匹配串中含有多少个模式串.注意模式串有重复,所以要累计重复结果. 解题 ...
- HDU2222 (AC自动机)
AC自动机模板题. 被卡内存了 死活A不掉.. AC自动机参考教程: http://www.cppblog.com/menjitianya/archive/2014/07/10/207604.html ...
随机推荐
- python-局域网内实现web页面用户端下载文件,easy!
好久没有发博客了,但是也没闲着,最近疫情原因一直在家远程办公,分享一下今天的干货 先说需求:某个文件压缩之后可以供用户点击下载 没想到特别好的办法,在网上搜索大多都是通过socket实现的,然后我这个 ...
- statement 、prepareStatement的用法和解释
转自:http://blog.csdn.net/QH_JAVA/article/details/48245945 一.prepareStatement 的用法和解释 1.PreparedState ...
- 广义高斯分布(GGD)和非对称广义高斯分布(AGGD)
<No-Reference Image Quality Assessment in the Spatial Domain>,BRISQUE. 1. 广义高斯分布,generalized G ...
- css笔记01
CSS样式(Cascading Style Sheets) 表格布局缺陷: 嵌套太多,一旦顺序错乱页面达不到预期效果 表格布局页面不灵活,动一块整个布局全都要变 语法: 在style标签中 ...
- zset底层数据结构
redis zset底层数据结构 https://www.jianshu.com/p/fb7547369655 跳跃列表(Skip List)与其在Redis中的实现详解 https://www.ji ...
- 洛谷 AT2827 LIS
题目传送门 解题思路: 用f[i]表示长度为i的最长上升子序列的最小的末尾. AC代码: #include<iostream> #include<cstdio> #includ ...
- EF Core开发模式之Code First
Code First顾名思义,代码为先.首先编写完相关的实体类及DbContext派生类,然后通过映射关系自动在数据库中完成数据库表的创建. 本例中创建一个班级和学生的管理,主要有班级类MyClass ...
- c++ 排序 冒泡 插入 选择 快速
//冒泡 #include <iostream> using namespace std; void bubbleSort(int* list,int index) { ;i--) //i ...
- 洛谷P1002 过河卒(动态规划)
题目描述 棋盘上 AA 点有一个过河卒,需要走到目标 BB 点.卒行走的规则:可以向下.或者向右.同时在棋盘上 CC 点有一个对方的马,该马所在的点和所有跳跃一步可达的点称为对方马的控制点.因此称之为 ...
- h5-sessionStorage储存的使用
<!-- sessionStorage的使用:存储数据到本地.存储的容量5mb左右 1.这个数据本质是储存在当前页面的内存中 2.他的生命周期为关闭当前页面,关闭页面,数据会自动清楚 setTt ...