01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package algs53; // section 5.3
import stdlib.*;
/* *************************************************************
 *  Compilation:  java KMPplus.java
 *  Execution:    java KMPplus pattern text
 *
 *  Knuth-Morris-Pratt algorithm over UNICODE alphabet.
 *
 *  % java KMPplus ABABAC BCBAABACAABABACAA
 *  text:    BCBAABACAABABACAA
 *  pattern:          ABABAC
 *
 *  % java KMPplus aabaaaba ccaabaabaabaaabaab
 *  text:    ccaabaabaabaaabaab
 *  pattern:         aabaaaba
 *
 *  % java KMPplus aabaaabb ccaabaabaabaaabaab
 *  text:    ccaabaabaabaaabaab
 *  pattern:                   aabaaabb
 *
 ***************************************************************/

public class XKMPplus {
  private final String pattern;
  private final int[] next;

  // create Knuth-Morris-Pratt NFA from pattern
  public XKMPplus(String pattern) {
    this.pattern = pattern;
    int M = pattern.length();
    next = new int[M];
    int j = -1;
    for (int i = 0; i < M; i++) {
      if (i == 0)                                      next[i] = -1;
      else if (pattern.charAt(i) != pattern.charAt(j)) next[i] = j;
      else                                             next[i] = next[j];
      while (j >= 0 && pattern.charAt(i) != pattern.charAt(j)) {
        j = next[j];
      }
      j++;
    }

    for (int i = 0; i < M; i++)
      StdOut.println("next[" + i + "] = " + next[i]);
  }

  // return offset of first occurrence of text in pattern (or N if no match)
  // simulate the NFA to find match
  public int search(String text) {
    int M = pattern.length();
    int N = text.length();
    int i, j;
    for (i = 0, j = 0; i < N && j < M; i++) {
      while (j >= 0 && text.charAt(i) != pattern.charAt(j))
        j = next[j];
      j++;
    }
    if (j == M) return i - M;
    return N;
  }


  // test client
  public static void main(String[] args) {
    //args = new String[] { "abracadabra", "abacadabrabracabracadabrabrabracad" };
    //args = new String[] { "rab",         "abacadabrabracabracadabrabrabracad" };
    //args = new String[] { "bcara",       "abacadabrabracabracadabrabrabracad" };
    //args = new String[] { "rabrabracad", "abacadabrabracabracadabrabrabracad" };
    args = new String[] { "abacad",      "abacadabrabracabracadabrabrabracad" };
    String pat = args[0];
    String txt = args[1];

    // substring search
    XKMPplus kmp = new XKMPplus(pat);
    int offset = kmp.search(txt);

    // print results
    StdOut.println("text:    " + txt);
    StdOut.print("pattern: ");
    for (int i = 0; i < offset; i++)
      StdOut.print(" ");
    StdOut.println(pat);
  }

}