KMP字符串匹配模式算法

主要看的是这两篇KMP博客:  https://www.cnblogs.com/kingofkai/p/6178773.html

https://blog.csdn.net/cdnight/article/details/11935387

部分匹配值是前缀(一定要包含第一个字符)和后缀(一定要包含最后一个字符)的公共子串

 j是失配位的前一位,是下面进行匹配的那一串匹配串(模式串)的下标

 

 求next数组即k值得代码;

    public static int[] kmpnext(String dest){
        int[] next = new int[dest.length()];
        next[0] = 0;
        for(int i = 1,j = 0; i < dest.length(); i++){
            while(j > 0 && dest.charAt(j) != dest.charAt(i)){
                j = next[j - 1];
            }
            if(dest.charAt(i) == dest.charAt(j)){
                j++;
            }
            next[i] = j;
        }
        return next;
    }

猜你喜欢

转载自blog.csdn.net/emmmsuperdan/article/details/81129380