[关闭]
@ysongzybl 2015-06-11T05:08:54.000000Z 字数 1926 阅读 806

KMP Algorithm Notes

interview


Compute Prefix Table

The prefix table of a string s, T[i] represents the length of the longest prefix of s that is also the suffix of s ends at index i (s[0...i])
Example :

  1. i : 0 1 2 3 4 5
  2. s : a b a c a b
  3. T : 0 0 1 0 1 2
  4. ________________________________
  5. | i | j | s[i] | s[j] | T[i] |
  6. --------------------------------
  7. | 0 | X | a | X | 0 |
  8. --------------------------------
  9. | 1 | 0 | b | a | 0 |
  10. | 2 | 0 | a | a | 1 |
  11. | 3 | 1 | c | b | | j = T[j-1] = 0, search prefix from the beginning (of course!)
  12. | 3 | 0 | c | a | 0 |
  13. | 4 | 0 | a | a | 1 |
  14. | 5 | 1 | b | b | 2 |
  15. --------------------------------
  1. public void computePrefixTable (int []T, String s){
  2. T[0] = 0;
  3. int i=1, j=0;
  4. int n = s.length();
  5. char []str = s.toCharArray();
  6. while(i<n){
  7. if(str[i]==str[j]){
  8. T[i] = j+1; // j is 0-based index, so j+1 is the length of prefix s[0..j]
  9. i++;
  10. j++;
  11. }else if(j>0){ // keep on search previously matched position
  12. j = T[j-1];
  13. }else{
  14. T[i] = 0; // re-match from the beginning
  15. i++;
  16. }
  17. }
  18. }

Implement StrStr()

  1. 1 1 1 1 1
  2. i : 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4
  3. s : a b c a b c d a b c d a b d e
  4. p : a b c d a b d
  5. j : 0 1 2 3 4 5 6
  6. T : 0 0 0 0 1 2 0
  7. ___________________________
  8. | i | j | s[i] | s[j] |
  9. ---------------------------
  10. | 0 | 0 | a | a |
  11. | 1 | 1 | b | b |
  12. | 2 | 2 | c | c |
  13. | 3 | 3 | a | d |
  14. --------------------------
  15. 1 1 1 1 1
  16. i : 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4
  17. s : a b c a b c d a b c d a b d e
  18. p : a b c d a b d
  19. j : 0 1 2 3 4 5 6
  20. T : 0 0 0 0 1 2 0
  21. __________________________
  22. | 3 | 0 | a | a | rematch p[0, m-1] from s[i-T[j-1],n-1], i fall back T[j-1]=0 steps
  23. | 4 | 1 | b | b |
  24. | 5 | 2 | c | c |
  25. | 6 | 3 | d | d |
  26. | 7 | 4 | a | a |
  27. | 8 | 5 | b | b |
  28. | 9 | 6 | c | d |
  29. -------------------------
  30. 1 1 1 1 1
  31. i : 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4
  32. s : a b c a b c d a b c d a b d e
  33. p : a b c d a b d
  34. j : 0 1 2 3 4 5 6
  35. T : 0 0 0 0 1 2 0
  36. __________________________
  37. | 7 | 0 | a | a | rematch p[0, m-1] from s[i-T[j-1],n-1], i fall back T[6-1]=2 steps
  38. | 8 | 1 | b | b |
  39. | 9 | 2 | c | c |
  40. | 10 | 3 | d | d |
  41. | 11 | 4 | a | a |
  42. | 12 | 5 | b | b |
  43. | 13 | 6 | d | d |
  44. -------------------------
  1. // p is the pattern, s is the string used to search for p
  2. // return start index in s if p is a substring of s, otherwise, -1
  3. public int strStr(String s, String p){
  4. int n = s.length();
  5. int m = p.length();
  6. if(m==0) return 0;
  7. int [] T = new int[m];
  8. computePrefixTable(T, p);
  9. int i=0, j=0;
  10. while(i<n){
  11. if(s.charAt[i]== p.charAt(j)){
  12. if(j==m-1) return i-j;
  13. i++;
  14. j++;
  15. }else if(j>0){ // i falls back T[j-1] steps, rematch from start index of p
  16. i= i- T[j-1];
  17. j= 0;
  18. }else{
  19. i++;
  20. }
  21. }
  22. return -1;
  23. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注