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

来源:互联网 发布:c语言 s和 c 编辑:程序博客网 时间:2024/05/21 18:36
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<lc;)         //   出口只有两个  应该是ci<lc
    {
        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++;



原创粉丝点击