字符串匹配算法(KMP)

作者: voltric | 来源:发表于2018-10-18 21:56 被阅读0次

String字符串匹配算法

@Date 2017.06.09

DEMO代码链接

暴力匹配

  • 时间复杂度O(m * n)
    private static int forceMatch(String originS, String matchedS) {

        char[] originArray = originS.toCharArray();
        char[] matchedArray = matchedS.toCharArray();

        int originLen = originS.length();
        int matchedLen = matchedS.length();

        int i = 0, j = 0;
        while (i < originLen && j < matchedLen) {
            if (originArray[i] == matchedArray[j]) {
                // 相等则继续位移比较(需要同时移位)
                i++;
                j++;
            } else {
                // 不相等则长字符串回到原点,重新比较
                i = i - j + 1;
                j = 0;
            }
        }
        // 匹配字符串完全找到
        if (j == matchedLen) {
            return i - j;
        } else {
            return -1;
        }
    }

KMP匹配

  • 时间复杂度O(m + n)
    /**
     * @param originS  长字符串
     * @param matchedS 要匹配的字符串
     * @return 匹配字符在长字符串中的位置
     */
    private static int kmpMatch(String originS, String matchedS) {
        // next 数组各值的含义:代表当前字符之前的字符串中,有多大长度的相同前缀后缀。
        // 例如如果next [j] = k,代表j 之前的字符串中有最大长度为k 的相同前缀后缀

        char[] originArray = originS.toCharArray();
        char[] matchedArray = matchedS.toCharArray();

        int originLen = originS.length();
        int matchedLen = matchedS.length();

        int[] next = getNext(matchedArray);

        int i = 0, j = 0;
        while (i < originLen && j < matchedLen) {
            if (j == -1 || originArray[i] == matchedArray[j]) {
                // 相等则继续位移比较(需要同时移位)
                i++;
                j++;
            } else {
                // j != -1,且当前字符匹配失败(originArray[i] != matchedArray[j]),则令 i 不变,j = next[j]
                // next[j]即为j所对应的next值
                // 失配时,模式串向右移动的位数为:已匹配字符数 - 失配字符的上一位字符所对应的最大长度值
                // 失配时,模式串向右移动的位数为:失配字符所在位置 - 失配字符对应的next 值
                j = next[j];
            }
        }
        // 匹配字符串完全找到
        if (j == matchedLen) {
            return i - j;
        } else {
            return -1;
        }
    }

    /**
     * 获取失配时的位移数组
     * 求解原理为:
     * 1. 匹配字符串的前缀后缀的公共元素的最大长度值的对应数组
     * 2. eg: abab : 0 0 1 2
     * 3. 最大对称长度的前缀后缀,然后整体右移一位,初值赋为-1
     * 4. eg: abab : -1 0 0 1
     * @param matchedArray 需要匹配的pattern字符串
     * @return 位移数组
     */
    private static int[] getNext(char[] matchedArray) {
        int matchedLen = matchedArray.length;
        int[] next = new int[matchedLen];
        next[0] = -1;
        int k = -1;
        int j = 0;
        while (j < matchedLen - 1) {
            // matchedArray[k]表示前缀,matchedArray[j]表示后缀
            // matchedArray[j] == matchedArray[k] 此判断是因为matchedArray[j]已经失配,用相同的matchedArray[k], 位移后去匹配一样是失配.
            // 故二者相等时,继续递归
            if (k == -1 || matchedArray[j] == matchedArray[k]) {
                ++k;
                ++j;
                next[j] = k;
            } else {
                k = next[k];
            }
        }
        return next;
    }

相关文章

  • KMP算法文章合集

    字符串的查找:朴素查找算法和KMP算法 暴力匹配算法与KMP算法(串的匹配) 字符串查找算法BF和KMP 字符串匹...

  • leetcode字符串匹配算法之KMP算法

    本篇介绍一种高效的字符串匹配算法——KMP算法。 KMP算法是一种改进的字符串匹配算法,由D.E.Knuth,J....

  • 字符串匹配与KMP算法

    1.朴素字符串匹配算法 2.KMP算法 求前缀函数 实现KMP算法 3.测试代码

  • 常见算法题之字符串

    1、KMP算法 参考:july大神的KMP博客细节不摆,该算法由暴力字符串来匹配,具体是由字符串匹配的暴力写法而来...

  • KMP算法(字符串匹配问题)

    一、是什么? 注意,是KMP算法,不是MMP哈,我没有骂人。KMP算法是用来做字符串匹配的,除了KMP算法分,还有...

  • KMP算法——寻找子串位置

    KMP算法——寻找子串位置 1、KMP算法简介: KMP算法是一种改进的字符串匹配算法,由D.E.Knuth,J....

  • KMP算法

    KMP算法是解决字符串匹配问题的有高效算法 代码:

  • KMP字符串匹配算法

    KMP字符串匹配算法 先总结一下之前的几种字符串匹配算法 1 BF算法, 最简单的字符串匹配算法, 可以直接使用s...

  • 09--KMP

    [toc] KMP算法原理 KMP思想 假设字符串abcdefgab和模式串abcdex,进行匹配,当匹配到x位置...

  • KMP算法理解

    文章大纲:1.KMP算法概念2.KMP算法中最核心的next[] 数组是如何生成的3.使用KMP算法 匹配字符串 ...

网友评论

    本文标题:字符串匹配算法(KMP)

    本文链接:https://www.haomeiwen.com/subject/exhezftx.html