AC自动机模板
2016-02-07 01:24
363 查看
第一次写得不好,大神们误喷...
#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=1010;
struct node{
int count,flag;
struct node *next[26],*fa,*fail;
char s1[110];
node(){
fail=fa=NULL;
count=flag=0;
memset(next,0,sizeof(next));
memset(s1,0,sizeof(s1));
}
};
struct node *root,*h,*fa,*tmp,*temp;
struct node *q[dmax*dmax];
char p[dmax][dmax];
char s[dmax*dmax];
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=1;
strcpy(h->s1,w);
}
void create_fail(){
int i,j,k,m,n,f=0,l=1;
q[1]=root;
while (f<l){
fa=q[++f];
For(i,0,25)
if (fa->next[i]!=NULL){
tmp=fa->fail;
while (tmp!=NULL && tmp->next[i]==NULL) tmp=tmp->fail;
fa->next[i]->fail=tmp==NULL?root:tmp->next[i];
q[++l]=fa->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];
if (h!=root){
tmp=h;
while (tmp!=NULL){
if (tmp->count && !tmp->flag){
printf("%s %d\n",tmp->s1,i-strlen(tmp->s1)+1);
tmp->flag=1;
}
tmp=tmp->fail;
}
}
}
}
int main(){
int i,j,k,m,n=0;
root=new node;
root->fail=NULL;
while (1){
gets(p[++n]);
if (p
[0]=='\0'){
n--;
break;
}
insert(p
);
}
gets(s);
create_fail();
auto_search(s);
return 0;
}
#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=1010;
struct node{
int count,flag;
struct node *next[26],*fa,*fail;
char s1[110];
node(){
fail=fa=NULL;
count=flag=0;
memset(next,0,sizeof(next));
memset(s1,0,sizeof(s1));
}
};
struct node *root,*h,*fa,*tmp,*temp;
struct node *q[dmax*dmax];
char p[dmax][dmax];
char s[dmax*dmax];
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=1;
strcpy(h->s1,w);
}
void create_fail(){
int i,j,k,m,n,f=0,l=1;
q[1]=root;
while (f<l){
fa=q[++f];
For(i,0,25)
if (fa->next[i]!=NULL){
tmp=fa->fail;
while (tmp!=NULL && tmp->next[i]==NULL) tmp=tmp->fail;
fa->next[i]->fail=tmp==NULL?root:tmp->next[i];
q[++l]=fa->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];
if (h!=root){
tmp=h;
while (tmp!=NULL){
if (tmp->count && !tmp->flag){
printf("%s %d\n",tmp->s1,i-strlen(tmp->s1)+1);
tmp->flag=1;
}
tmp=tmp->fail;
}
}
}
}
int main(){
int i,j,k,m,n=0;
root=new node;
root->fail=NULL;
while (1){
gets(p[++n]);
if (p
[0]=='\0'){
n--;
break;
}
insert(p
);
}
gets(s);
create_fail();
auto_search(s);
return 0;
}
相关文章推荐
- [1701]: ACMer
- 分布式缓存Memcached
- [java]final关键字、finally关键字与finalize()方法
- 通过一个小程序看你C语言语法掌握了多少
- 【Android开发小记--11】调用百度地图定位功能
- Kinect For Windows V2开发日志四:使用OpenCV显示深度图像
- HDU 1114 Piggy-Bank(完全背包)
- 【翻译自mos文章】Standby Redo Logs (SRL)的用途,益处与限制
- mongoose 文档(四) queries
- hdu1207 做题中的反思与总结
- C++大牛的博客
- 初识AngularJS
- 算法导论例程——计数排序
- 2.7
- 开头很艰难,但也要写下去
- 新闻客户端之欢迎界面和新手引导界面
- 自发行python版本制作(一)
- 01背包问题一维空间优化的理解
- 3-3 wordcount代码编写 mapper+reducer,wordcount
- Java通过SpyMemcached来缓存数据