字符串查找之KMP


根据定义next[0]=-1,假设next[j]=k, 即P[0...k-1]==P[j-k,j-1]

   1)若P[j]==P[k],则next[j+1]=k+1;

   2)若P[j]!=P[k],则k=next[k]。


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

void get_next(char *pat, int next[])
{
        int i, j, plen;

        plen = strlen(pat);
        i = 0;
        j = -1;
        next[0] = -1;

        while(i < plen-1){
                if(j == -1 || pat[i] == pat[j]){
                        ++i;
                        ++j;
                        next[i] = j;
                }
                else{
                        j = next[j];
                }
        }
}

int kmp_match(char *str, char *pat)
{
        int slen, plen, i, j;
        int *next;

        if(str == NULL || pat == NULL){
                return -1;
        }

        slen = strlen(str);
        if(slen <= 0){
                return -1;
        }

        plen = strlen(pat);
        if(plen <= 0){
                return -1;
        }

        if(slen < plen){
                return 0;
        }

        next = (int *)malloc(plen*sizeof(int));
        if(next == NULL){
                return -1;
        }

        get_next(pat, next);

        i = 0;
        j = 0;
        while(i < slen){
                if(j == -1 || str[i] == pat[j]){
                        i++;
                        j++;
                }
                else{
                        j = next[j];
                }

                if(j == plen){
                        free(next);
                        return (i-plen);
                }
        }

        free(next);
        return 0;
}


int main(int argc, char *argv[])
{
        int ret;

        if(argc != 3){
                printf("usage: a.out [patter] [string] \n");
                return -1;
        }

        ret = kmp_match(argv[2], argv[1]);
        printf("ret = %d\n", ret);

        return 0;
}


你可能感兴趣的:(String,null)