kmp算法 c语言(转)

/*
 * kmp.c
 *
 *  Created on: 2012-1-5
 *      Author: admin
 */

#include <stdio.h>
#include <string.h>
int index_KMP(char *s,char *t,int pos);
void get_next(char *t,int *);

char s[10]="abcacbcba";
char t[4]="abca";
int next[4];
int pos=0;

int main()
{
    int n;
    get_next(t,next);
    n=index_KMP(s,t,pos);
    printf("%d",n);
    return 0;
}

int index_KMP(char *s,char *t,int pos)
{
    int i=pos,j=1;
    while (i<=(int)strlen(s)&&j<=(int)strlen(t))
    {
        if (j==0||s[i]==t[j-1])
        {
            i++;
            j++;
        }
        else j=next[j];
    }
    if (j>(int)strlen(t))
        return i-strlen(t)+1;
    else
        return 0;
}

void get_next(char *t,int *next)
{

    int i=1,j=0;
    next[0]=next[1]=0;
    while (i<(int)strlen(t))
    {
        if (j==0||t[i]==t[j])
        {
            i++;
            j++;
            next[i]=j;
        }
        else j=next[j];
    }

}

/**************************************************************************************/

#include<stdio.h>
#include<malloc.h>
#include<string.h>
#include<assert.h>
#include<stdlib.h>
void getNext(const char * t,int * Next)//get the Next array 
{
 int k=-1;
 int j=0;
 int size=strlen(t);
 Next[0]=-1;
 while(j<size)
 {
  if(k==-1||t[j]==t[k])//if k==-1 there are two conditions 
   //one is this is the first time entering the loop
  {   //if t[j]==t[k] get the next[j+1] directly
   k++;//the other is the end of the iteration cos k==-1;
   j++;
   Next[j]=k;//whatever Next[j]=k  
  }
  else
   k=Next[k];
 }
}
int myStrstr(const char * Dest,const char *subStr)//find the starting position of the sub
{                                                 //in the Dest through KMP
 int destSize=strlen(Dest);
 int subSize=strlen(subStr);
 int i,j;
 int * Next=(int *)(malloc(sizeof(int)*subSize));
 i=j=0;
 assert((Dest!=NULL)&&(subStr!=NULL));
 getNext(subStr,Next);
 while(i<destSize&&j<subSize)
 {
  if(j==-1||Dest[i]==subStr[j])//if j==-1 the main string need match the next elements
  {                            //and the subString begin from the beginning
   i++;                     //if Dest[i]==subStr[j]  both string need shift to the
   j++;                     // next elements
  }
  else
   j=Next[j];                   //if match fail,glide back to Next[j]
 }
 if(j==subSize)return i-j;
 return -1;
}
int main()
{
 char *temp,*sub,* Dest;//to store the substring to be matched
 int ch;
 unsigned int templen,length=20*sizeof(char);
 unsigned int mlength=20*sizeof(char);//the original length of the memory
 sub=(char*)malloc(length);
 Dest=(char*)malloc(length);
 if(sub==NULL||Dest==NULL)//allocation failure
 {
  printf("memory allocate failure\n");
  exit(0);
 }
 temp=sub;
 printf("please input the substring:\n");
 while((ch=getchar())!=10)//read the sub String
 {
  if((temp-sub)/sizeof(char)==length)//if running out of the memory
  {
   templen=length;
   sub=realloc(sub,length+=20*sizeof(char));
   if(sub==NULL)
   {
    printf("sub memory allocate failure\n");
    exit(0);
   }
   temp=sub+templen*sizeof(char);//reset the temp cos the sub may change its value
  }
  *temp++=ch;
 }
 *temp='\0';
 temp=Dest;
 printf("please input the mainstring:\n");
 while((ch=getchar())!=10)//read the main String
 {
  if((temp-Dest)/sizeof(char)==mlength)
  {
   templen=mlength;
   Dest=realloc(Dest,mlength+=20*sizeof(char));//if running out of the memory
   if(Dest==NULL)
   {
    printf("sub memory allocate failure\n");
    exit(0);
   }
   temp=Dest+templen*sizeof(char);//reset the temp cos the Dest may change its value
  }
  *temp++=ch;
 }
 *temp='\0';
 printf("the starting position is :%d\n",myStrstr(Dest,sub));//get the starting position
 free(Dest);
 free(sub);
 return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值