模式匹配朴素算法习题 算法导论P559

int Index_for_CLRS(char*f,char*c)
{
    int s,fi,ci,lf,lc;
    s=0;
    lf=strlen(f);
    lc=strlen(c);


    for(fi=ci=0;s<=lf-lc&&ci     {
        printf("%d\n",s);
        if(f[fi]==c[ci])
        {
            fi++;
            ci++;
        }
        else
        {
            s+=ci>1?ci:1;        //这里要注意  答案有误
            fi=s;
            ci=0;
        }
    }
    if(s>lf-lc) return 0;//出口1:位移超过了
    else  return s+1;

}


32:1 - 2
Assume all the characters of P are different. A mismatch with T a position i of P in line 4 of
NAIVE-STRING-MATCHER then implies that mean that we can continue our search from position
s + i in T. Thus a linear search of T is sufcient.

if(i=0)  then  s++;



你可能感兴趣的:(模式匹配朴素算法习题 算法导论P559)