朴素算法与KMP(java)

朴素匹配算法

        朴素算法也称暴力解法, 比如: 主串"abbdhhheee", 子串为"hhh", 我们如何查出主串中是否包含子串?

        朴素字符串匹配的思路非常简单,我们需要在s1中找到不否包含s2,那么可以遍历s1的每个字符位置,然后向后匹配s2字符串长度的字符,发现不匹配时,转去考虑目标串里的下一个位置是否与模式串匹配。

public class A {
    public static void main(String[] args) {

        String a = "qasdwertr";
        String b = "ert";

        int index = Index(a, b);
        System.out.println(index);

    }

    public static int Index(String n, String m){
        int k = 0;
        int i = 0, j = 0;

        while (i <= n.length() - 1 && j <= m.length() - 1) {

            char a = n.charAt(i), b = m.charAt(j);
            if(a == b){
                i++;
                j++;
            }
            else {
                k++;
                i = k;
                j = 0;
            }
        }

        if(j <= m.length()) return k;
        else return 0;
    }
}

        朴素匹配算法非常简单,容易理解,但其效率极低。造成其低效的主要因素是执行中可能出现回溯:匹配中遇到一对字符不同时,模式串p将向右移一个字符的位置,随后的匹配回到模式串的开始(重置 j=0),也回到目标串中前面的下一个位置,重新开始比较字符。最坏的情况是每一趟比较都在模式串的最后遇到了字符不匹配的情况,在这种匹配中总共需要n-m+1趟比较,总的比较次数为m×(n-m+1),所以这个算法的复杂度为O(m×n)。

KMP算法

        KMP算法在朴素算法的基础上,需要多计算一个next数组.next数组中存的值其实就是最长相同前缀后缀的长度。在进行字符串匹配时,如果P[i] != Q[j],其实这也是第一次不匹配,这说明了之前的j-1个字符都匹配上了,对于这j-1个字符构成的字符串S,我们只需要从S的最长相同前缀后缀的长度处开始下一轮比较即可,不需要再回退i。

        每一个字符前的字符串都有最长相等前后缀,而且最长相等前后缀的长度是我们移位的关键。所以我们需要构造一个pnext列表存储子串的最长相等前后缀的长度,pnext[i]表示下标为i 的字符前的字符串最长相等前后缀的长度。

 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数组的值之后,就可以开始进行字符串匹配了.

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 class Kmp {

    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 了

    }

    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;
    }
}

        

        

        

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CuteTTU

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值