HDU2222 AC自动机水题
2016-02-09 15:57
393 查看
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<iostream> #include<algorithm> #define For(i,j,k) for (i=j;i<=k;i++) using namespace std; const int dmax=100100; struct node{ int count,flag; struct node *next[26],*fail,*fa; node(){ count=flag=0; fail=fa=NULL; memset(next,0,sizeof(next)); } }; struct node *root,*h,*tmp,*fa; struct node *q[60*10100]; char p[10100][60]; char s[1000100]; int ans; void insert(char *w){ int i,k,n=strlen(w); h=root; For(i,0,n-1){ k=w[i]-97; if (h->next[k]==NULL){ tmp=new node; tmp->fa=h; h->next[k]=tmp; } h=h->next[k]; } h->count++; } void create_fail(){ int i,f=0,l=1; q[1]=root; while (f<l){ h=q[++f]; For(i,0,25) if (h->next[i]!=NULL){ tmp=h->fail; while (tmp!=NULL && tmp->next[i]==NULL) tmp=tmp->fail; h->next[i]->fail=tmp==NULL?root:tmp->next[i]; q[++l]=h->next[i]; } } } void auto_search(char *s){ int i,j,k,m,n=strlen(s); h=root; For(i,0,n-1){ k=s[i]-97; while (h!=NULL && h->next[k]==NULL) h=h->fail; h=h==NULL?root:h->next[k]; tmp=h; while (tmp!=root){ if (!tmp->flag && tmp->count){ ans+=tmp->count; tmp->flag=1; } tmp=tmp->fail; } } } void dfs(struct node *f){ int i; For(i,0,25) if (f->next[i]!=NULL) dfs(f->next[i]); delete f; } int main(){ int i,j,k,m,n,T; scanf("%d",&T); while (T--){ root=new node; root->fail=NULL; ans=0; scanf("%d",&n); getchar(); For(i,1,n){ gets(p[i]); insert(p[i]); } gets(s); create_fail(); auto_search(s); printf("%d\n",ans); dfs(root); } return 0; }
相关文章推荐
- mysql 使用基本教程
- TCP协议与UDP协议的区别
- 详解AngularJS中的http拦截
- Java XML分析技术: StAX, SAX, DOM, DOM4j, JDOM
- HDU 1264 Counting Squares(模拟)
- google推出的SwipeRefreshLayout下拉刷新用法
- epoll example
- 构建Linux内核驱动demo子系统示例
- PHP - json生成与转换
- 5.4、聚类之EM聚类实例
- Java认证考试实例疑难辨析(2)
- 同余式运算的总结
- 360Wifi2代在linux上安装使用 踩过的的坑和成功的途径 分享一下
- JSP学习小结
- 断开式连接批量更新数据库数据(grid编辑)
- jetbrains
- HDU 3452 Bonsai
- [PHP]如何在百度(BAE)和新浪(SAE)的云平台使用PHP连接MySQL并返回结果数据
- 如何划分子网
- 【慕课笔记】U3 继承 第6节 JAVA中的Object类(一)