KMP算法

时间:2020-07-16 12:09:41   收藏:0   阅读:51
1.涉及知识部分匹配表
A 0
AB 前缀A 后缀B 公共部分长度0
ABA
前缀:A AB 后缀:BA A 公共部分A,长度为1
[0,0,1]



package com.hy.tenalgorithm;

import java.util.Arrays;

/**
* @author hanyong
* @date 2020/7/16 1:04
*/
public class KMPAlgorithm {
public static void main(String[] args) {

String str1 = "BBC ABCDAB ABCDABCDABDE";
String str2 = "ABCDABD";
//String str2 = "BBC";

int[] next = kmpNext("ABCDABD"); //[0, 1, 2, 0]
System.out.println("next=" + Arrays.toString(next));

int index = kmpSearch(str1, str2, next);
System.out.println("index=" + index); // 15了

}

/**
* @param str1 源字符串
* @param str2 子串
* @param next 子串的部分匹配表
* @return
*/
public static int kmpSearch(String str1, String str2, int[] next) {

for (int i = 0, j = 0; i < str1.length(); i++) {
while (j > 0 && str1.charAt(i) != str2.charAt(j)) {
j = next[j - 1];
}
if (str1.charAt(i) == str2.charAt(j)) {
j++;
}
if (j == str2.length()) {
return i - j + 1;
}
}
return -1;
}

/**
* 获取一个字符串的部分匹配值表
*
* @param str
* @return
*/
public static int[] kmpNext(String str) {
int[] next = new int[str.length()];
next[0] = 0;

for (int i = 1, j = 0; i < str.length(); i++) {
while (j > 0 && str.charAt(i) != str.charAt(j)) {
j = next[j - 1];
}
if (str.charAt(i) == str.charAt(j)) {
j++;
}
next[i] = j;
}

return next;
}
}
评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!