HDU 2222 : Keywords Search
发布日期:2021-06-30 16:05:52 浏览次数:2 分类:技术文章

本文共 774 字,大约阅读时间需要 2 分钟。

学习了字典树之后,来学一下AC自动机,其实挺简单的,但是很强大

这是一道AC自动机模板题
AC自动机学习博客:

我的代码:

#include
#include
using namespace std;const int maxn=500000+100;int trie[maxn][26],sum[maxn],fail[maxn],tot;int que[maxn];inline void insert(char* ch){ int len=strlen(ch); int root=0; for(int i=0;i
=0){ cnt+=sum[node_tmp]; sum[node_tmp]=-1; } else break; node_tmp=fail[node_tmp]; } } } return cnt;}inline void init(){ tot=0; memset(trie,0,sizeof(trie)); memset(sum,0,sizeof(sum)); memset(fail,0,sizeof(fail));}char str[maxn*2];int main(){ int T,n; scanf("%d",&T); while(T--){ init(); scanf("%d",&n); getchar(); char ch[100]; for(int i=1;i<=n;i++){ scanf("%s",ch); insert(ch); } build_fail(); scanf("%s",str); printf("%d\n",search(str)); }}

转载地址:https://kaven.blog.csdn.net/article/details/81169791 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:HDU 1711 : Number Sequence
下一篇:HDU 1671 : Phone List

发表评论

最新留言

很好
[***.229.124.182]2024年04月25日 12时51分14秒