public class StringMatcherAlgorithm {
/**
* indicates the largest possible shift S if mismatching the pattern.
* @param pattern the pattern you want to search for in above text,like a word.
* @return the length of the longest prefix of P that is a suffix of P[i . . j].
*
*/
//text: a b a c a a b a c c
//pattern: a b a c a b
static int[] failure(char[] pattern){
//length of the previous longest prefix suffix
int m = pattern.length;
int[] f = new int[m];
int i = 1;
int j = 0;
f[0] = 0;
while(i < m){
if(pattern[i] == pattern[j]){
f[i] = j+1;
i = i+1;
j = j+1;
}else if(j > 0){
j = f[j-1];
}else{
f[i] = 0;
i = i+1;
}
}
return f;
}
/**
* Knuth-Morris-Pratt algorithm.
* @param text the text such as a sentence,an article and so on.
* @param pattern the pattern you want to search for in above text,like a word.
* @return the index of the pattern in the text if match the pattern
*/
public static List<Integer> KMP(char[] text, char[] pattern){
int[] f = failure(pattern);
int n = text.length;
int m = pattern.length;
List<Integer> list = new ArrayList<Integer>();
int i = 0;
int j = 0;
while(i < n){
if(text[i] == pattern[j]){
i = i+1;
j = j+1;
}
if(j == m){//find pattern
list.add(i-j);
j = f[j-1];//the pattern slide to the next window
}
else if(i < n && text[i] != pattern[j]){
if(j != 0)
j = f[j-1];
else//j == 0 indicate the first character of the pattern mismatch the character ,text[i]
i = i+1;
}
}
return list;
}
}
Reference:
http://www.personal.kent.edu/~rmuhamma/Algorithms/MyAlgorithms/StringMatch/kuthMP.htmhttps://www.geeksforgeeks.org/searching-for-patterns-set-2-kmp-algorithm/
http://jakeboxer.com/blog/2009/12/13/the-knuth-morris-pratt-algorithm-in-my-own-words/
http://www.iti.fh-flensburg.de/lang/algorithmen/pattern/kmpen.htm