KMP算法学习

20170801update:当时竟然没有强调next函数的含义?

next[i]的含义是,i之前的整个前缀中,最长的该前缀的前缀和后缀相同的长度。

看图:

KMP感觉是我学到现在最难懂的一个算法了QAQ 为什么你们都那么强啊,看几个小时就看懂了…

先放一波我觉得值得看的资料: kmp算法讲解(配图比较全….) kmp学习资料2

说下我对kmp算法的理解:

理解kmp算法大概分成两个部分。

一部分是理解一个naive的kmp算法,可以叫"fast slide" algorithm

思想大概就是,当mismatch发生时,我们并不是一无所有,而是知道在mismatch发生前所匹配的字符的信息的。

然后知道这些信息我们可以做什么呢?

先观察一下最最暴力的求解字符串匹配的算法:

 1//***********************************************************
 2//brute force 
 3 n = T.length();
 4      m = P.length();
 5
 6      i0 = 0;              // Line P up with the first character of T       
 7      i = 0;               // Start matching with first char in T
 8      j = 0;               // Start matching with first char in P
 9
10      while ( i < n )     // Not all characters used
11      {
12         if ( T[i] == P[j] )
13         {
14            /* ===============================================
15	       T[i] and P[j] match ==> try next pair
16	       =============================================== */
17            i++;           // Match next pair
18            j++;
 1            if ( j == m )
 2               return ( i0 );    // Match found at position i0 !!!
 3         }
 4         else
 5         {  /* ===========================================
 6               T[i]  P[j]:
 7                  1. Slide P up 1 position
 8		  2. restart from beginning of string
 9               =========================================== */
10            i0 = i0 + 1;   // Slide pattern P one character further
1            i  = i0;       // Restart matching at position i0 in T
2            j  = 0;        // Restart matching at position 0 in P
3         }
4      }
5
6      return -1;           // Return not found
7   }

这个算法低效在,当mismatch发生时,我只是往前移动了一个字符。

Screenshot from 2016-08-08 21-03-22

因此我们定义了 maxoverlap函数。。

Screenshot from 2016-08-08 21-05-51

需要特别强调的是:

Screenshot from 2016-08-08 21-08-22

这样我们就可以利用maxoverlap函数来优化当mismatch发生的时候,移动的过程。

 1  KMP( T,  P )
 2   {
 3      int i0, i, j, m, n;
 4
 5      n = T.length();
 6      m = P.length();
 7
 8      i0 = 0;              // Line P up with the first character of T
 9      i = 0;               // Start matching with first char in T
10      j = 0;               // Start matching with first char in P
11
12      while ( i < n )     // Not all characters used
13      {
14         if ( T[i] == P[j] )
15         {
16            i++;           // Match next pair
17            j++;
18
19            if ( j == m )
20               return ( i0 );    // Match found atposition i0 !!!
21         }
22         else
23         {  /* ===========================================
24	       T[i] ≠ P[j]
25               =========================================== */
26
27             if ( j == 0 )
28	     {  /* ==============================================
29	           First character already mismatched
30		   We have NO prefix info. to work with...
31		   =============================================== */
32	        i0++;        // Just slide P 1 character over
33		i = i0;      //
34		j = 0;
35             }
36	     else
37             {
38	        prefix = P[ 0..(j-1) ];  // Prefix of pattern at the mismatch    
39
40	        k = MaxOverlap( prefix );
41
42	        j = k;
43	        i0 = (i - j);
44		// i is unchanged !
45             }
46         }
47      }
48
49      return -1;           // No match found
50   }

这样我们就得到了**“fast slide” algorithm算法**

但是。。。能不能再给力一点呢

再给力一点

我们发现,上述代码中,我们每次都要计算一次maxoverlap函数…

但是。 。一个字符串。。。它的子串个数是有限的。。。

我们能不能加速这个过程呢。。。

很容易想到预处理一波。。。

也就是求所谓的failure function ,中文应该叫失配函数…

Screenshot from 2016-08-08 21-19-04

所以第二部分就是如何快速求出失配函数

这也是我认为的kmp算法最精(nan)髓(dong)的地方。