您的位置:首页 > 其它

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;

     
     
     
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: