字符串匹配算法:KMP算法与BM算法比较

    

          在字符串匹配算法中,比较有名有的KMP算法和BM算法。本文将从学习的角度来比较这两种算法的易理解与易掌握性。

 

         (1)KMP算法

                 KMP算法的关键在于算出Next集。在计算Next集时,有以下几种情况:

                  设Next[j] = K, 则有  a1....ak-1 = aj-k+1....aj-1 :

                     a)  若 ak = aj,   则有 a1... a= aj-k+1 ....  a j,即

                                Next[j+1] = K+1  -------------------》      Next[j+1]  = Next[j] + 1

                     b)  若 ak != aj,又分为两种情况:

                            *   设Next[k] =k' ,  且 a[k'] = a[j].

 

                  

                    // 下面这个函数是引用《数据结构》C语言版中的例子,其中pattern是模式串,以1为下标

                    void get_nextval( char*  pattern,  int  nlen, int* pnextval )

                   {

                              int   i = 1;

                              int   j = 0;

                              pnextval[1] = 0;

                              while( i < nlen )

                              {

                                      if( j == 0  || pattern[i] == pattern[j] )

                                      {

                                                 ++i;

                                                 ++j;

                                                 if( pattern[i] == pattern[j] )

                                                       pnextval[i]  = j;

                                                  else

                                                       pnextval[i] = pnextval[j];

                                      }

                                       else

                                                  j = pnextval[j];

                               }

                      }

                      int   KmpSearch( char*  buf, int blen, char*  pattern,  int plen,  int* pnextval,  int pos )

                      {

                                   int  i = pos;

                                   int  j = 0;

                                   while(  j < plen  &&  i < blen )

                                   {

                                               if( j == 0  || buf[i] == pattern[j] )

                                               {

                                                             i++;

                                                             j++;

                                                }

                                               else

                                                       j = pnextval[j];

                                    }

                      

                                    if( j >= plen )

                                               return   i - plen;

                                    return 0;

                       }

                    ......未完待续

                            

         

你可能感兴趣的:(技术性文章)