当前位置:网站首页>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服务设置
- How to switch remote server in gerrit
- 无主复制系统(1)-节点故障时写DB
- How Redis handles concurrent access
- [TypeScript] In-depth study of TypeScript type operations
- AcWing 1282. 搜索关键词 题解((AC自动机)Trie+KMP)+bfs)
- Golang 必知必会Go Mod命令
- Baidu cloud web speed playback (is there any website available)
- 无主复制系统(3)-Quorum一致性的局限性
- Anaconda如何顺利安装CV2
猜你喜欢

js的toString方法

Smart Trash Can (8) - Infrared Tube Sensor (Raspberry Pi pico)

The 2nd China PWA Developer Day

Three aspects of Ali: How to solve the problem of MQ message loss, duplication and backlog?

Automated testing - web automation - first acquaintance with selenium

C程序是如何跑起来的01 —— 普通可执行文件的构成
![[Meetup Preview] OpenMLDB+OneFlow: Link feature engineering to model training to accelerate machine learning model development](/img/f6/311d5a4c70993df6291250d2025d3f.jpg)
[Meetup Preview] OpenMLDB+OneFlow: Link feature engineering to model training to accelerate machine learning model development

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

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

LevelSequence源码分析
随机推荐
go图书管理系统
牛客网刷题(三)
使用 Postman 工具高效管理和测试 SAP ABAP OData 服务的试读版
[TypeScript]OOP
Replication Latency Case (1) - Eventual Consistency
How Redis handles concurrent access
Implementing distributed locks based on Redis (SETNX), case: Solving oversold orders under high concurrency
Dialogue with Zhuang Biaowei: The first lesson of open source
仿生毛毛虫机器人源码
Qt practical cases (54) - using transparency QPixmap design pictures
复制延迟案例(1)-最终一致性
最后写入胜利(丢弃并发写入)
GP 6 overall architecture study notes
单细胞测序流程(单细胞rna测序)
多数据中心操作和检测并发写入
并发性,时间和相对性
认识异常 (看完这篇你就懂了)
动态规划之线性dp(下)
第05章 存储引擎【1.MySQL架构篇】【MySQL高级】
Anaconda如何顺利安装CV2