hdu2222 ac自动机模板
来源:互联网 发布:细菌发电数据 编辑:程序博客网 时间:2024/06/05 09:51
ac自动机模板题
#include<iostream>#include<cstdio>#include<cstring>#include<string>#include<cstdlib>#include<cmath>#include<ctime>#include<algorithm>#include<iomanip>#include<cctype>#include<queue>using namespace std;const int N=1e4+5,L=55,M=1e6+5;struct node{ int cnt,fail,son[26];}tr[N*L];char s[M];int T,tot,n;bool visit[N*L];queue<int>q;void Insert(){ int len=strlen(s),po=1; for(int i=0;i<len;i++) { if(!tr[po].son[s[i]-'a']) tr[po].son[s[i]-'a']=++tot; po=tr[po].son[s[i]-'a']; } tr[po].cnt++;}void buildfail(){ q.push(1); while(!q.empty()) { int u=q.front(); q.pop(); for(int i=0;i<26;i++) { int v=tr[u].fail,w; while(!tr[v].son[i]) v=tr[v].fail; v=tr[v].son[i],w=tr[u].son[i]; if(w)tr[w].fail=v,q.push(w); else tr[u].son[i]=v; } }}int auto_ac(){ int len=strlen(s),po=1,ans=0,tmp; for(int i=0;i<len;i++) { po=tr[po].son[s[i]-'a']; tmp=po; while(tmp&&!visit[tmp]) { ans+=tr[tmp].cnt; visit[tmp]=true; tmp=tr[tmp].fail; } } return ans;}int main(){ //freopen("lx.in","r",stdin); scanf("%d",&T); while(T--) { memset(tr,0,sizeof(tr)); memset(visit,0,sizeof(visit)); tot=1; for(int i=0;i<26;i++)tr[0].son[i]=1; scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%s",s); Insert(); } buildfail(); scanf("%s",s); cout<<auto_ac()<<endl; } return 0;}
阅读全文
0 0
- AC自动机模板 hdu2222
- hdu2222 ac自动机模板
- HDU2222 AC自动机模板
- AC自动机模板 hdu2222
- hdu2222 ac自动机模板
- ac自动机模板hdu2222
- hdu2222 ac自动机模板题
- AC自动机模板(hdu2222)
- ac自动机模板(hdu2222)
- ac自动机模板(hdu2222)
- HDU2222 AC自动机 入门模板
- hdu2222 AC自动机入门 指针型模板
- AC自动机模板(数组+指针)hdu2222
- HDU2222 Keywords Search AC自动机模板题
- HDU2222 Keywords Search(AC自动机模板题)
- hdu2222--Keywords Search+AC自动机模板
- hdu2222&hdu3065 AC自动机模板题
- HDU2222 Keywords Search [AC自动机模板]
- 对Linux中inode的感悟
- Android (拍照功能)
- 使用apidoc 生成Restful web Api文档
- [转]彻底解决WPS弹出热点广告、WPS购物图标的办法
- caffe跑试验遇到错误:Check failed: error == cudaSuccess (2 vs. 0) out of memory
- hdu2222 ac自动机模板
- 中断上下文不能做的事情
- 前端试题大综合练习(二)
- css实现footer底部自粘
- 20170721LINK
- openstack手动搭建
- 多组数据的处理
- HDU 2017
- opencv检测中线