KMP算法之next数组的求解初级版

首先, 看一下计算初级版next数组的算法 :

// 该函数的作用是获取匹配数组t的next的数组, 始终操作的是t数组 :

void get_next(char *t,int *next)  

{  

    // 因为操作的是同一个数组, i 开始得和 j 错开 :  

    int i=0;  
    int j=1;  
    next[1]=0;
    while(j<t[0]-'0')      // j小于t数组的长度 .
    {  
        if(0==i || t[i]==t[j])  
        {  
            i++;  
            j++;  
            next[j] = i;
        }  
        else   // i 进行回溯 
        {  
            i=next[i];  
        }  

    }

}





猜你喜欢

转载自blog.csdn.net/i_pangpang/article/details/80384562
今日推荐