Stduy_Student 发表于 2014-5-7 11:59:10

KMP算法代码实现

#include <stdio.h>
#include <string.h>
int index_KMP(char *s,char *t,int pos);
void get_next(char *t,int *);
char s="abcacbcba";
char t="bca";
int next;
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==t)      
                  {            
                          i++;            
                          j++;      
                  }
                  else
                          j=next;
    }
          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=next=0;   
          while (i<(int)strlen(t))   
          {      
                  if (j==0||t==t)      
                  {            
                          i++;            
                          j++;            
                          next=j;      
                       }      
                  else
                          j=next;   
                }
}

Stduy_Student 发表于 2014-5-7 12:00:25

自己顶一个了

Stduy_Student 发表于 2014-5-7 12:01:38

自己再来一个了

小小的鳄鱼 发表于 2014-7-17 13:28:59

我来顶一个

Angel丶L 发表于 2014-7-22 14:13:32

来看看支持下。
页: [1]
查看完整版本: KMP算法代码实现