HDU2222 AC自动机水题
来源:互联网 发布:农村淘宝服务站申请 编辑:程序博客网 时间:2024/04/29 01:44
#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;}
1 0
- HDU2222 AC自动机水题
- hdu2222 ac自动机模板题
- hdu2222 ac自动机裸题
- HDU2222 AC自动机水题 (Trie图优化)
- AC自动机的初步学习 hdu2222 AC自动机入门题
- 我的ac自动机第一题 hdu2222
- HDU2222 Keywords Search AC自动机裸题
- HDU2222 Keywords Search AC自动机模板题
- HDU2222 Keywords Search(AC自动机模板题)
- hdu2222&hdu3065 AC自动机模板题
- AC自动机简单题 hdu2222+poj2778
- hdu2222 Keywords Search(AC自动机模板题)
- HDU2222[Keywords Search] AC自动机裸题
- HDU2222 Keywords Search 【AC自动机模板题】
- HDU2222(AC自动机入门题)
- hdu2222 ac自动机模版
- hdu2222 AC自动机
- hdu2222,(ac自动机)
- epoll example
- 5.4、聚类之EM聚类实例
- hdu2243 考研路茫茫——单词情结 (AC自动机+矩阵快速幂)
- Java XML分析技术: StAX, SAX, DOM, DOM4j, JDOM
- c语言window 磁盘清理
- HDU2222 AC自动机水题
- TCP协议与UDP协议的区别
- mysql 使用基本教程
- [BZOJ2588] Spoj 10628. Count on a tree
- 曼哈顿距离、欧氏距离
- 团队建设(Team building)就是等于组织大家一起吃饭娱乐吗?
- 一个android文本比对app的实现(四)--Hirschberg文本比对算法
- 72.windbg-命令行选项(command-line options)
- Creating and Manipulating Files and Directories创建和处理文件和目录