当前位置:网站首页>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;
}
边栏推荐
- 你辛辛苦苦写的文章可能不是你的原创
- tensorflow2.0 cnn(layerwise)
- 【7.29】代码源 - 【排列】【石子游戏 II】【Cow and Snacks】【最小生成数】【数列】
- 网站漏洞修复服务商关于越权漏洞分析
- npm安装时卡在sill idealTree buildDeps,npm安装速度慢,npm安装卡在一个地方不动
- server certificate verification failed. CAfile: /etc/ssl/certs/ca-certificates.crt CRLfile: none 失败
- 最后写入胜利(丢弃并发写入)
- TypeError: unhashable type: ‘list‘
- After Effects 教程,如何在 After Effects 中调整过度曝光的快照?
- 智能垃圾桶(九)——震动传感器(树莓派pico实现)
猜你喜欢
随机推荐
MySQL多表联合查询
单细胞测序流程(单细胞rna测序)
第05章 存储引擎【1.MySQL架构篇】【MySQL高级】
研发过程中的文档管理与工具
LevelSequence源码分析
2020 WeChat applet decompilation tutorial (can applet decompile source code be used)
无主复制系统(3)-Quorum一致性的局限性
多数据中心操作和检测并发写入
【愚公系列】2022年07月 Go教学课程 022-Go容器之字典
【pytorch】1.7 pytorch与numpy,tensor与array的转换
Premiere Pro 2022 for (pr 2022)v22.5.0
最后写入胜利(丢弃并发写入)
The 2nd China PWA Developer Day
【TypeScript】深入学习TypeScript类型操作
The arm button controls the flashing of the led light (embedded button experiment report)
无主复制系统(2)-读写quorum
苹果官网样式调整 结账时产品图片“巨大化”
Go1.18升级功能 - 模糊测试Fuzz 从零开始Go语言
How to install CV2 smoothly in Anaconda
仿生毛毛虫机器人源码