【AC自动机】ACW 1282. 搜索关键词
2021/4/26 10:27:35
本文主要是介绍【AC自动机】ACW 1282. 搜索关键词,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; const int N = 10010, S = 55, M = 1000010; int n; int tr[N * S][26], cnt[N * S], idx; char str[M]; int q[N * S], ne[N * S]; void insert() { int p = 0; for (int i = 0; str[i]; i ++ ) { int t = str[i] - 'a'; if (!tr[p][t]) tr[p][t] = ++ idx; 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() { int T; scanf("%d", &T); while (T -- ) { memset(tr, 0, sizeof tr); memset(cnt, 0, sizeof cnt); memset(ne, 0, sizeof ne); idx = 0; scanf("%d", &n); for (int i = 0; i < n; i ++ ) { scanf("%s", str); insert(); } build(); scanf("%s", str); int res = 0; for (int i = 0, j = 0; str[i]; i ++ ) { int t = str[i] - 'a'; j = tr[j][t]; int p = j; while (p) { res += cnt[p]; cnt[p] = 0; p = ne[p]; } } printf("%d\n", res); } return 0; }
这篇关于【AC自动机】ACW 1282. 搜索关键词的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-09flutter3.x_macos桌面os实战
- 2024-05-09Rust中的并发性:Sync 和 Send Traits
- 2024-05-08使用Ollama和OpenWebUI在CPU上玩转Meta Llama3-8B
- 2024-05-08完工标准(DoD)与验收条件(AC)究竟有什么不同?
- 2024-05-084万 star 的 NocoDB 在 sealos 上一键起,轻松把数据库编程智能表格
- 2024-05-08Mac 版Stable Diffusion WebUI的安装
- 2024-05-08解锁CodeGeeX智能问答中3项独有的隐藏技能
- 2024-05-08RAG算法优化+新增代码仓库支持,CodeGeeX的@repo功能效果提升
- 2024-05-08代码报错不用愁,CodeGeeX一键完成代码修复、错误解释的功能上线了!
- 2024-05-08今天开始程序员不用再发愁写commit message了,全部由CodeGeeX自动完成!