当前位置:网站首页>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;
}
边栏推荐
- 使用 Postman 工具高效管理和测试 SAP ABAP OData 服务的试读版
- 牛客网刷题(三)
- Small program: Matlab solves differential equations "recommended collection"
- Applicable Scenarios of Multi-Master Replication (1) - Multi-IDC
- 并发性,时间和相对性
- Go1.18升级功能 - 模糊测试Fuzz 从零开始Go语言
- Summary of the implementation method of string inversion "recommended collection"
- The arm button controls the flashing of the led light (embedded button experiment report)
- 【TypeScript】深入学习TypeScript类型操作
- t-sne 数据可视化网络中的部分参数+
猜你喜欢

T - sne + data visualization parts of the network parameters
![[pytorch] 1.7 pytorch and numpy, tensor and array conversion](/img/ca/b943ff8f59f08e9e23b1ba416c79a0.png)
[pytorch] 1.7 pytorch and numpy, tensor and array conversion

【7.29】Code Source - 【Arrangement】【Stone Game II】【Cow and Snacks】【Minimum Number of Spawns】【Sequence】

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

Qt实战案例(54)——利用QPixmap设计图片透明度

t-sne 数据可视化网络中的部分参数+

AcWing 1282. 搜索关键词 题解((AC自动机)Trie+KMP)+bfs)

Golang——从入门到放弃

C language "the third is" upgrade (mode selection + AI chess)

研发过程中的文档管理与工具
随机推荐
研发过程中的文档管理与工具
牛客 HJ3 明明的随机数
复杂高维医学数据挖掘与疾病风险分类研究
Golang go-redis cluster模式下不断创建新连接,效率下降问题解决
多主复制下处理写冲突(3)-收敛至一致的状态及自定义冲突解决逻辑
阿里三面:MQ 消息丢失、重复、积压问题,如何解决?
Three aspects of Ali: How to solve the problem of MQ message loss, duplication and backlog?
【7.29】代码源 - 【排列】【石子游戏 II】【Cow and Snacks】【最小生成数】【数列】
2020微信小程序反编译教程(小程序反编译源码能用吗)
After the form is submitted, the page does not jump [easy to understand]
[pytorch] pytorch automatic derivation, Tensor and Autograd
动态规划(一)
[Source code analysis] BeanFactory and FactoryBean
[pytorch] 1.7 pytorch and numpy, tensor and array conversion
无主复制系统(2)-读写quorum
【7.28】代码源 - 【Fence Painting】【合适数对(数据加强版)】
[TypeScript]OOP
Last write wins (discards concurrent writes)
动态规划之线性dp(下)
server certificate verification failed. CAfile: /etc/ssl/certs/ca-certificates.crt CRLfile: none 失败