7月6号 ac自动机问题

做下kmp,和字典树,回忆一下自己从前做过的知识~~~

貌似~~~忘记的差不~~多了~~码了两道题~~写一下~~

http://acm.hdu.edu.cn/showproblem.php?pid=1711

hdu 1711 上面的题~~~

post code 直接贴代码~~~

#include<stdio.h>
int len1,len2,nextval[11000],pos;
int a[1000010],b[11000];
void getnext();

int main()
{
  int i,j,n;
 scanf("%d",&n);
 while(n--)
 {
 scanf("%d",&len1);
 scanf("%d",&len2);
 
 for(i=0;i<len1;i++)
 scanf("%d",&a[i]);
 
 for(i=0;i<len2;i++)
 scanf("%d",&b[i]);
 getnext();

  i=0;j=0;
   while(i<len1&&j<len2)
   {
   if(j==-1||a[i]==b[j]){i++;j++;}
   else j=nextval[j];  
   }
 if(j>=len2)pos=i-len2+1;
 else pos=-1;
 
 printf("%d\n",pos);
 }

}

void getnext()
{
int j,k;
j=0;
k=-1;
nextval[0]=-1;
 while(j<len2)
 {
  if(k==-1||b[j]==b[k])
  {
    j++;k++;
    if(b[j]!=b[k])nextval[j]=k;
    else nextval[j]=nextval[k];
  }
  else k=nextval[k];
 }     
}

字典树的知识:http://acm.hdu.edu.cn/showproblem.php?pid=1251

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

struct node{
           struct node *next[26];
           int count ;
           } *p;
char a[20];
void maketire();
void searchtire();

int main()
{  
    int i;
    p=(struct node *)malloc(sizeof(struct node));
    for(i=0;i<26;i++)
    p->next[i]=NULL;
    while(gets(a)&&a[0]!='\0')
      maketire();
    while(gets(a))
      searchtire();
}

void maketire()
{
  int len,i,j;
  struct node *q,*temp;
  len=strlen(a);
  q=p;
  for(i=0;i<len;i++)
    {    
        
      if(q->next[a[i]-'a']==NULL)
      {
        temp=(struct node*)malloc(sizeof(struct node));
        for(j=0;j<26;j++)
           temp->next[j]=NULL;
        temp->count=0;
          
        q->next[a[i]-'a']=temp;            
      }

      q=q->next[a[i]-'a'];
      q->count++;
   
    } 
}

void searchtire()
{
  int len,i,flag=0;
  struct node *t=p;
  len=strlen(a);     
   for(i=0;i<len;i++)
   {
      if(t->next[a[i]-'a']!=NULL)
        {
          t=t->next[a[i]-'a'];
        }       
      else {printf("0\n");flag=1;break;}          
   }  
    if(flag==0)  printf("%d\n",t->count);  
}

下面就是ac自动机问题:

转载于:https://www.cnblogs.com/ysh-blog/archive/2012/07/07/2580944.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值