KMP可以用来做什么?
KMP主要应用在字符串匹配上,当出现字符串不匹配的情况时,可以知道一些以前匹配过的信息,利用信息去避免重复匹配,如何去记录已经匹配过的内容就是KMP算法的核心,也是next数组的意义所在。
next数组是什么?
next数组就是前缀表,记录主串与所匹配串在不匹配的时候,应该从哪儿重新匹配的位置 如:在 aadaadaacq 字符串里面找到 aadaac
使用前缀表,可以避免暴力匹配从头开始。
记录原字符串的字母出现次数从0开始记录如aadaa 在数组中就表示为[0,1,0,1,2]每断开一次,就得从上次的记录开始,示例:aadaadaacq、aadaac
| 原字符 | 长度 |
|---|---|
| a a d a a d | 0 1 0 1 2 0 |
| 匹配字符 | 下标 |
| --- | --- |
| a a d a a c | 0 1 2 3 4 5 |
在末尾匹配d、c的时候字母不同,所以就得用原字符d的前一位a字符的长度2,在匹配符中寻找下标为2的字符d,然后位移到匹配符d继续向下进行匹配aac,可以发现用这样的方式遍历完后就找出了aadaac这个子串
如何用代码去创建next数组呢?
public void getNext(String str){
int []next = new int[str.length];
next[0] = 0; //初始化
int j = 0; //双指针判断 i始终比j大
for(int i =1;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;
}
//next数组就构建完毕了
}
一个leetcode题--找出字符串中第一个匹配项的下标:
代码:
class Solution {
public int strStr(String haystack, String needle) {
if (needle.length() == 0) return 0;
int[] next = new int[needle.length()];
getNext(next, needle);
int j = 0;
for (int i = 0; i < haystack.length(); i++) {
while (j > 0 && needle.charAt(j) != haystack.charAt(i))
j = next[j-1];
if (needle.charAt(j) == haystack.charAt(i)) j++;
if (j == needle.length()) return i - j + 1;
}
return -1;
}
public void getNext(int[] next, String str) {
next[0] = 0;
int j = 0;
for (int i = 1; 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;
}
}
}
菜鸟入门哭唧唧