HDU 3065 病毒侵袭持续中(AC自动…
2012-12-05 17:15
330 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3065
分析见上一篇博客
直接附代码:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef
struct
point{
int count;
struct point *fail,*next[26];
}*Tree,Node;
Tree root,Q[50008];
char
str[2000008];
int
num[1008];
Tree NEW()
{
int i;
Tree p;
p=(Tree)malloc(sizeof(Node));
for(i=0;i<26;i++)
p->next[i]=NULL;
p->fail=NULL;
p->count=-1;
return p;
}
void
Build(char ss[],int u)
{
int i=0;
Tree p,s;
p=root;
while(ss[i])
{
if(p->next[ss[i]-'A']==NULL)
{
s=NEW();
p->next[ss[i]-'A']=s;
p=s;
}
else p=p->next[ss[i]-'A'];
i++;
}
p->count=u;
}
void
AC_Automation()
{
int i,front=0,rear=0;
Tree p,temp;
root->fail=NULL;
Q[front++]=root;
while(rear<front)
{
temp=Q[rear++];
p=NULL;
for(i=0;i<26;i++)
{
if(temp->next[i]!=NULL)
{
if(temp==root)temp->next[i]->fail=root;
else
{
p=temp->fail;
while(p!=NULL)
{
if(p->next[i]!=NULL)
{
temp->next[i]->fail=p->next[i];
break;
}
p=p->fail;
分析见上一篇博客
直接附代码:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef
struct
point{
int count;
struct point *fail,*next[26];
}*Tree,Node;
Tree root,Q[50008];
char
str[2000008];
int
num[1008];
Tree NEW()
{
int i;
Tree p;
p=(Tree)malloc(sizeof(Node));
for(i=0;i<26;i++)
p->next[i]=NULL;
p->fail=NULL;
p->count=-1;
return p;
}
void
Build(char ss[],int u)
{
int i=0;
Tree p,s;
p=root;
while(ss[i])
{
if(p->next[ss[i]-'A']==NULL)
{
s=NEW();
p->next[ss[i]-'A']=s;
p=s;
}
else p=p->next[ss[i]-'A'];
i++;
}
p->count=u;
}
void
AC_Automation()
{
int i,front=0,rear=0;
Tree p,temp;
root->fail=NULL;
Q[front++]=root;
while(rear<front)
{
temp=Q[rear++];
p=NULL;
for(i=0;i<26;i++)
{
if(temp->next[i]!=NULL)
{
if(temp==root)temp->next[i]->fail=root;
else
{
p=temp->fail;
while(p!=NULL)
{
if(p->next[i]!=NULL)
{
temp->next[i]->fail=p->next[i];
break;
}
p=p->fail;
相关文章推荐
- HDU 2222 Keywords Search(AC自…
- HDU 2896 病毒侵袭(AC自动机)
- HDU-2896 病毒侵袭 && HDU-3065 病毒侵袭持续中(AC自动机)
- HDU 1219 AC Me
- Hdu 2457 DNA repair (字符串_AC自…
- HDU 3901 Wildcard AC主动机通配符…
- hdu&nbsp;4082&nbsp;Hou&nbsp;Yi&#039;s&nbsp;secret&nbsp;枚举
- HDU:3117 Fibonacci Numbers
- ZZULI_SummerPractice(3) HDU 1…
- HDU 动态规划(46道题目)倾情奉献
- HDU 2680 Choose the best route
- HDU 4160 (ZZULI 1598) Dolls(二分…
- HDU 4251 The Famous ICPC Team Ag…
- hdu 相遇周期
- hdu 1016 Prime Ring Problem
- hdu 1081 To The Max
- Android ViewPager自动播放
- 关于SQL Server自动增长字段
- HDU&nbsp;2546&nbsp;饭卡(01背包)
- HDU&nbsp;4279&nbsp;Number