KMP(Knuth–Morris–Pratt) Algorithm

文章 String S, 要查找的字符串 String W

重点:Get int T[] from W

用变量 c 来表示当前比较到的 W 中前面的字符,
i 表示当前要处理的 T 中的 index,
int[] T = new int[W.length];
T[0] = -1; T[1] = 0;
c=0; i=2

example:
array:    a    b    a    b    c    d    a    b    a    b    a    b
idx    :    0    1    2    3    4    5    6    7    8    9   10  11

case 1:  W[c] == W[i-1] --> T[i] = ++c; i++;
case 2:  W[c] != W[i-1] && c>0 --> c = T[c];
case 3:  W[c] != W[i-1] && c==0 --> T[i] = 0; i++;

过程
array: a    b    a    b    c    d    a    b    a    b    a    b
idx   : 0    1    2    3    4    5    6    7    8    9   10  11

i: 2 3 4 5 5 6 7 8 9 10 11 11
case: 3 1 1 2 3 3 1 1 1 1 2 1
pre-c: 0 0 1 2 0 0 0 1 2 3 4 2
after-c: 0 1 2 0 0 0 1 2 3 4 2 3
比较: 0 & 0 0 & 2 1 & 3 2 & 4 0 & 4 0 & 5 0 & 6 1 & 7 2 & 8 3 & 9 4 & 10 2 & 10

 

Related Problems: Implement strStr()Shortest Palindrome

 

最后 po 一段自己写的 leetcode 上 implement strStr() 的代码

 1 public class Solution {
 2     public int strStr(String haystack, String needle) {
 3         if (needle == null || needle.length() == 0) return 0;
 4         
 5         int ndlen = needle.length();
 6         int hslen = haystack.length();
 7         
 8         if (ndlen == 1) {
 9             // only need to find the character in heystack
10             return strChar(haystack, needle.charAt(0));
11         }
12         
13         int[] table = buildKMPTable(needle);
14         
15         // KMP algorithm based on helper table
16         int ndidx = 0, hsidx = 0;
17         while (hsidx < hslen) {
18             if (haystack.charAt(hsidx) == needle.charAt(ndidx)) {
19                 hsidx++;
20                 ndidx++;
21                 if (ndidx == ndlen) return hsidx - ndlen;
22             } else if (table[ndidx] != -1) {
23                 ndidx = table[ndidx];
24             } else {
25                 hsidx++;
26             }
27         }
28         return -1;
29     }
30     
31     private int strChar(String str, char c) {
32         if (str == null || str.length() == 0) return -1;
33         for (int i = 0; i < str.length(); i++) {
34             if (str.charAt(i) == c) return i;
35         }
36         return -1;
37     }
38     
39     /* KMP table[i] contain the idx position that 
40      * when str[j] != word[i], compare str[j] & word[table[i]]
41      */
42     private int[] buildKMPTable(String needle) {
43         int[] table = new int[needle.length()];
44         
45         // needle.length() > 1
46         table[0] = -1;
47         table[1] = 0;
48         int idx = 0;
49         int i = 2;
50         while(i < needle.length())  {
51             char pre = needle.charAt(idx);
52             char cur = needle.charAt(i - 1);
53             if (pre == cur) { // case 1. word[idx] == word[i-1] 
54                 table[i++] = ++idx;
55             } else {
56                 if (idx == 0) { // case 2. word[idx] != word[i-1] && idx == 0
57                     table[i++] = 0;
58                 } else { // case 3. word[idx] != word[i-1] && idx > 0
59                     idx = table[idx];
60                 }
61             }
62         }
63         return table;
64     }
65 }

 

posted @ 2015-04-26 06:15  Joyce-Lee  阅读(278)  评论(0编辑  收藏  举报