zoukankan      html  css  js  c++  java
  • KMP算法C语言实现。弄了好久才搞好。。。

    我的这个算法中数组的第一位没有像教材中那样用来存数组的大小,所以会有些许的不同。
    // KMP算法
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    void get_next(char *T,int next[])	//修正前的next数组 
    {
    int i = 1,j = 0;
    next[0] = -1;
    next[1] = 0;
    int m = strlen(T);
    while(i<strlen(T)-1)
    {
    if(j == -1||T[j]==T[i])
    {
    ++i;
    ++j;
    next[i] = j;
    }
    else j = next[j];
    }
    } 
    
    void get_nextval(char *T,int nextval[])	//修正后的nextval数组
    {
    int i = 1,j = 0;
    nextval[0] = -1;
    nextval[1] = 0;
    int m = strlen(T);
    while(i<strlen(T)-1)
    {
    if(j == -1||T[j]==T[i])
    {
    ++i;
    ++j;
    if(T[i]!=T[j]) nextval[i] = j;
    else nextval[i] = nextval[j];
    }
    else j = nextval[j];
    }
    } 
    
    int Index_kmp(char *S,char *T,int pos,int next[])	//逐项比较 
    {
    int j = 0,i = pos,lens=strlen(S),lent=strlen(T);
    get_next(T,next);
    while(i<lens&&j<lent)
    {
    if(S[i]==T[j]||j==-1)
    {
    i++;j++;
    }
    else j = next[j];
    }
    if(j>=lent) return i-lent;
    else return -1;
    }
    
    int main()
    {
    char *S="adbadabbaabadabbadada",*T="adabbadada";
    int m;
    int *next = (int *)malloc((strlen(T)+1)*sizeof(int));	//修正前的next数组
    int *nextval = (int *)malloc((strlen(T)+1)*sizeof(int));	//修正后的nextval数组
    
    get_next(T,next);
    printf("修正前next数组为:");
    for(m = 0;m<strlen(T);m++)
    {
    printf("%d ",next[m]+1);
    }
    
    get_nextval(T,nextval);
    printf("\n修正后的nextval数组为:");
    for(m=0;m<strlen(T);m++)
    {
    printf("%d ",nextval[m]+1);
    }
    
    
    int t = Index_kmp(S,T,0,nextval);
    if(t==-1) printf("\n无匹配项!\n");
    else
    {
    printf("\n在第%d项开始匹配成功\n",t+1);
    }
    return 0;
    }
    

      

  • 相关阅读:
    mvc是如何工作的
    MVC4 AJAX Unobtrusive
    MVC4 jQuery UI自动完成
    MVC4Html Helper
    MVC4 Layouts布局视图局部视图
    理解mvc
    ASP.NET MVC HTMLHELPER类的方法总结
    I2C中的重复起始条件到底是什么意思
    release, retain, autorelease 与 AT, MT, AMT
    CMSIS SVD(System View Description)小解
  • 原文地址:https://www.cnblogs.com/marsggbo/p/6622937.html
Copyright © 2011-2022 走看看