标签:next数组 res while 查找 lan 索引 length 遇到 发表
KMP是一个解决模式串在文本串是否出现过,若出现过,最早出现的位置的算法
Knuth-Morris-Pratt 字符串查找算法,简称“KMP算法”,此算法由 Donald Knuth、Vaughan Pratt、James H. Morris 三人于 1977年联合发表,故使用三人姓氏命名
KMP方法利用之前判断过的信息,new 一个数组,保存模式串前后最长公共子序列的长度,每次回溯时,通过next数组找到,前面匹配过的位置,省去了大量的实践
str1 = “IMUTIMUTIMUTIMUT”
str2 = “IMUTIMU”
判断 str1 中是否包含 str2,若存在,则返回第一次出现的位置,没有则返回 -1
package cn.imut;
@SuppressWarnings("all")
public class ViolenceMatch {
    public static void main(String[] args) {
        String str1 = "MUIMUTIMUTIMUTIMUT";
        String str2 = "IMUTIMU";
        int index = violenceMatch(str1,str2);
        System.out.println(index);
    }
    public static int violenceMatch(String str1, String str2) {
        char[] ch1 = str1.toCharArray();        //字符串转换为字符数组
        char[] ch2 = str2.toCharArray();
        int ch1Len = ch1.length;
        int ch2Len = ch2.length;
        int i = 0;  //索引,指向 ch1
        int j = 0;  //指向 ch2
        while (i < ch1Len && j < ch2Len) {
            if(ch1[i] == ch2[j]) {  //匹配ok
                i++;
                j++;
            }else { //不成功
                i = i - (j - 1);
                j = 0;
            }
        }
        //判断是否成功
        if(j == ch2Len) {
            return i - j;
        }else {
            return -1;
        }
    }
}
package cn.imut;
import java.util.Arrays;
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 如果是-1就是没有匹配到,否则返回第一个匹配的位置
     */
    public static int kmpSearch(String str1, String str2, int[] next) {
        //遍历
        for(int i = 0, j = 0; i < str1.length(); i++) {
            //需要处理 str1.charAt(i) != str2.charAt(j), 去调整j的大小
            //KMP算法核心点, 可以验证...
            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()) {//找到了 // j = 3 i
                return i - j + 1;
            }
        }
        return  -1;
    }
    //获取到一个字符串(子串) 的部分匹配值表
    public static  int[] kmpNext(String dest) {
        //创建一个next 数组保存部分匹配值
        int[] next = new int[dest.length()];
        next[0] = 0; //如果字符串是长度为1 部分匹配值就是0
        for(int i = 1, j = 0; i < dest.length(); i++) {
            //当dest.charAt(i) != dest.charAt(j) ,我们需要从next[j-1]获取新的j
            //直到我们发现 有  dest.charAt(i) == dest.charAt(j)成立才退出
            //这时kmp算法的核心点
            while(j > 0 && dest.charAt(i) != dest.charAt(j)) {
                j = next[j-1];
            }
            //当dest.charAt(i) == dest.charAt(j) 满足时,部分匹配值就是+1
            if(dest.charAt(i) == dest.charAt(j)) {
                j++;
            }
            next[i] = j;
        }
        return next;
    }
}
标签:next数组 res while 查找 lan 索引 length 遇到 发表
原文地址:https://www.cnblogs.com/yfyyy/p/12812231.html