KMP算法
kmp算法是在字符串匹配目前效率最高的算法。传统的字符串匹配算法时间复杂度为O(m * n),而kmp算法将时间复杂度简化到了O(m + n),利用了O(n) 的空间复杂度。
其中,求出next数组是KMP算法的关键
// The premier procedure in KPM
// Set array next
private static void setNext(String matched, int[] next) {
next[0] = -1;
int j = 0;
int k = -1;
while (j < matched.length() - 1) {
// matched.charAt(j) is prefix
// matched.charAt(k) is postfix
if (k == -1 || matched.charAt(j) == matched.charAt(k)) {
++ j;
++ k;
next[j] = k;
}
else {
k = next[k];
}
}
System.out.println(Arrays.toString(next));
}
例如ABCDABD对应next:
程序实现如下:
import java.util.Arrays;
/**
* Author : WindAsMe
* File : kpmMatch.java
* Time : Create on 18-6-15
* Location : ../Home/JavaForLeeCode2/kpmMatch.java
* Function : Learning String matched algorithm
*/
public class kpmMatch {
// KPM matched Algorithm:
// 1. Calculate the next Array
// 2. Match and move the text
// 3. IF match the part of matched and the next is unmatched:
// j = next[j] + 1 (j is index the matched)
// 4. IF j == matched.length() - 1, match ending, success.
private static int kpmMatchResult(String text, String matched) {
if (text.length() < matched.length()) {
return -1;
}
int[] next = new int[matched.length()];
setNext(matched, next);
int j = 0;
for (int i = 0 ; i < text.length() ; i ++ ) {
System.out.println("i:" + i + " j:" + j);
if (text.charAt(i) == matched.charAt(j)) {
j ++;
if (j == matched.length() - 1) {
return i - j + 1;
}
} else {
if (j != 0) {
j = next[j] + 1;
}
}
}
return -1;
}
// The premier procedure in KPM
// Set array next
private static void setNext(String matched, int[] next) {
next[0] = -1;
int j = 0;
int k = -1;
while (j < matched.length() - 1) {
// matched.charAt(j) is prefix
// matched.charAt(k) is postfix
if (k == -1 || matched.charAt(j) == matched.charAt(k)) {
++ j;
++ k;
next[j] = k;
}
else {
k = next[k];
}
}
System.out.println(Arrays.toString(next));
}
public static void main(String[] args) {
System.out.println(kpmMatchResult("abcweaabcdabcqqababc", "abcabc"));
}
}