String matching problem - KMP algorithm

Table of contents

Introduction to KMP Algorithm

Best Application of KMP Algorithm - String Matching Problem

Idea analysis diagram

KMP string matching code implementation:


Introduction to KMP Algorithm

1) KMP is a classic algorithm to solve whether the pattern string has appeared in the text string, and if so, the earliest position

2) The KMP method algorithm uses the previously judged information, through a next array, saves the length of the longest common subsequence in the pattern string, keeps i from backtracking, and modifies the position of j through the next array, so that the substring can be moved to as far as possible A valid location to match.

3) References: https://www.cnblogs.com/ZuoAndFutureGirl/p/9028287.html

Best Application of KMP Algorithm - String Matching Problem

String matching problem: :

1) There is a string str1= "BBC ABCDAB ABCDABCDABDE", and a substring str2="ABCDABD"

2) Now it is necessary to judge whether strl contains str2, if it exists, return the first occurrence position, if not, return -1

3) Requirement: Use the KMP algorithm to complete the judgment

Idea analysis diagram

 

 

KMP string matching code implementation:

public class ViolenceMatch {
    public static void main(String[] args) {//字符串匹配算法

        String str1 = "安全出口 出口安全 安全安全出口安安安安全全出口出口 ";
        String str2 = "安全出口安安";
        //测试KMP算法
        int[] next = KmpNext(str2);
        System.out.println(Arrays.toString(next));//[0, 0, 0, 0, 1, 1]
        int index2 = KmpSearch(str1, str2, next);
        System.out.println("KMP算法:index=" + index2);
    }

    /**
     * kmp算法实现
     *
     * @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的大小
            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;//没找到
    }

    //获取到一个字符串[子串]的部分匹配表
    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(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;
    }
}

 

Guess you like

Origin blog.csdn.net/m0_52729352/article/details/121948650