首页 > 其他 > 详细

Knuth-Morris-Pratt Algorithm

时间:2014-03-22 22:32:22      阅读:517      评论:0      收藏:0      [点我收藏+]

  Today , 第一次学习KMP Algorithm,其中好多地方还是不能理解的透彻,本文将进一步对 KMP Algorithm 进行学习,搞清楚其中的思想……

  First , KMP Algorithm is best known for liner time for exact matching ,  (Runing time is O(Length(S)+Lendth(P)))  Because Preprocessing is O(P) , Matching is O(Length(S)) , 效率很 high , 成功的避免了Recomputing Matches ;

    若想 Avoid Recomputing Matches , 就需要 Preprocessing ,对于 Preprocessing ,就是找出字符串P中的 Repeat char can backtrack position by prefix-function;

字符串P为:

a b a b a c a

 

 

字符串S为:

 

b a c b a b a b a b a c a a b

 

 

  定义两个指针 i 和 j ,i  是指向 字符串 S 中的第 i 个数据元素 , j 是指向字符串 P 中的第 j 个数据元素 ,用指针 i 和  j 分别表示 S[ i - j + 1 , …… i ] 和 P[ 1 , …… j ] 中的数据元素完全相等, 随着 i 的值不断增加 , j 的值也在不断变化 ,通过对字符串 P 进行 Preprocessing 得到 next数组 ,j 的值变化有两种可能:1、如果 P[ j + 1 ] 与 S[ i + 1]相等,j 应该加 1 ; 2 、如果 P[ j + 1 ] 与 S[ i + 1 ] 不相等 ,则 j 应该等于 next 数组中第 j 个数据元素的值 ;next 数组就是用来记录 当P[i + 1] 与 S[ j + 1 ] 不相等时 , j 的变化 ;

 

下面介绍一下,Prefix - function ,即如何确定next 数组 ;

m ← Length[ P ] ;

k = 0 ;

next[1] ← 0 ;

for q ← 2 to m  do

  while k > 0 and p[k+1] ≠ p[q]  do

    k = next[k] ;

  end while 

  if(p[k+1] = p[q] )

    k ← k + 1 ;

  end if

  next[q] = k ;

end for

 

 

这样即可得到 nex t数组 ;   

 

得到 next 数组之后,就可进行字符串P 与 字符串S 进行匹配了 ,具体匹配过程,下面给出相应的伪代码:

n ← Length[ S ] 

m ← Length[ P ]

k = 0 ;

for i ← 1 to n  do 

  while k > 0 and p[ k+1 ] ≠  S[ i ]  do

    k = next[ k ] ;

  end while

  if p[k+1] = S[i]  then

    k ← k + 1

  end if

  if k == m then  

    return true

  end if

end for 

 

 

 

 

 

 

      

Knuth-Morris-Pratt Algorithm,布布扣,bubuko.com

Knuth-Morris-Pratt Algorithm

原文:http://www.cnblogs.com/scottding/p/3603527.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!