当前位置:网站首页>AcWing 1282. Search Keyword Problem Solution ((AC Automata) Trie+KMP)+bfs)
AcWing 1282. Search Keyword Problem Solution ((AC Automata) Trie+KMP)+bfs)
2022-07-31 16:40:00 【QingQingDE23】
AcWing 1282. 搜索关键词((AC自动机)Trie+KMP)+bfs)
An algorithm that I wanted to learn a long time ago,But I didn't understand it before,Good thing I got it this time
希望ACAutomata can help meAC吧
#include<bits/stdc++.h>
using namespace std;
const int N = 1e4 + 10, S = 55;
int cnt[N * S], tr[N * S][26], ne[N * S];
int n, T, ids;
int q[N * S];
string str;
void insert(){
int p = 0;
for(int i = 0; str[i]; i ++ ){
int t = str[i] - 'a';
if(!tr[p][t]) tr[p][t] = ++ ids;
p = tr[p][t];
}
cnt[p] ++ ;
}
void build(){
int hh = 0, tt = -1;
for(int i = 0; i < 26; i ++ ){
if(tr[0][i]) q[ ++ tt] = tr[0][i];
}
while(hh <= tt){
int t = q[hh ++ ];
for(int i = 0; i < 26; i ++ ){
int p = tr[t][i];
if(!p) tr[t][i] = tr[ne[t]][i];
else{
ne[p] = tr[ne[t]][i];
q[ ++ tt] = p;
}
}
}
}
int main()
{
cin>>T;
while(T -- ){
cin>>n;
memset(cnt, 0, sizeof cnt);
memset(ne, 0, sizeof ne);
memset(tr, 0, sizeof tr);
ids = 0;
int res = 0;
for(int i = 0; i < n; i ++ ){
cin>>str;
insert();
}
build();
cin>>str;
for(int i = 0, j = 0; str[i]; i ++ ){
int t = str[i] - 'a';
j = tr[j][t]; //代表从j节点连到tbranch of node,j是从根节点开始
int p = j;
while(p){
res += cnt[p];
cnt[p] = 0;
p = ne[p];
}
}
cout<<res<<endl;
}
return 0;
}
边栏推荐
- 【网络通信三】研华网关Modbus服务设置
- Golang 必知必会Go Mod命令
- Premiere Pro 2022 for (pr 2022)v22.5.0
- After Effects 教程,如何在 After Effects 中调整过度曝光的快照?
- Go1.18升级功能 - 模糊测试Fuzz 从零开始Go语言
- 多主复制下处理写冲突(1)-同步与异步冲突检测及避免冲突
- The 2nd China PWA Developer Day
- 自动化测试—web自动化—selenium初识
- How to switch remote server in gerrit
- i.MX6ULL driver development | 33 - NXP original network device driver reading (LAN8720 PHY)
猜你喜欢

IP协议从0到1

你辛辛苦苦写的文章可能不是你的原创

复杂高维医学数据挖掘与疾病风险分类研究

adb shell 报错error: device unauthorized

外媒所言非虚,苹果降价或许是真的在清库存

【7.29】代码源 - 【排列】【石子游戏 II】【Cow and Snacks】【最小生成数】【数列】
![[pytorch] 1.7 pytorch and numpy, tensor and array conversion](/img/ca/b943ff8f59f08e9e23b1ba416c79a0.png)
[pytorch] 1.7 pytorch and numpy, tensor and array conversion

智能垃圾桶(八)——红外对管传感器(树莓派pico)

组合学笔记(六)局部有限偏序集的关联代数,Möbius反演公式

Dialogue with Zhuang Biaowei: The first lesson of open source
随机推荐
牛客网刷题(一)
.NET 20th Anniversary Interview - Zhang Shanyou: How .NET technology empowers and changes the world
动态规划(一)
牛客网刷题(四)
SringMVC中个常见的几个问题
Dialogue with Zhuang Biaowei: The first lesson of open source
最新神作!阿里巴巴刚出炉的面试参考指南(泰山版),我直接狂刷29天
Flutter 获取状态栏statusbar的高度
动态规划之线性dp(上)
研发过程中的文档管理与工具
【pytorch】1.7 pytorch与numpy,tensor与array的转换
SHELL内外置命令
研发过程中的文档管理与工具
2.索引及调优篇【mysql高级】
[TypeScript] In-depth study of TypeScript type operations
AcWing 1282. 搜索关键词 题解((AC自动机)Trie+KMP)+bfs)
牛客 HJ16 购物单
go图书管理系统
Single-cell sequencing workflow (single-cell RNA sequencing)
[Source code analysis] BeanFactory and FactoryBean