KMP算法

KMP算法

应用场景-字符串匹配问题

举例:判断两个字符数组是否匹配,匹配的话,返回出现位置的下标

暴力匹配

代码示例:

package com.wxit.kmp;

/**
 * @Author wj
 * 暴力匹配算法
 **/
public class ViolenceMatch {

    public static void main(String[] args) {
        //测试暴力匹配算法
        String str1 = "轻轻的风儿轻轻地吹";
        String str2 = "风儿";                                                                 

        int index = violenceMatch(str1, str2);
        System.out.println("index=" + index);
    }

    //暴力匹配算法的实现
    public static int violenceMatch(String str1,String str2){
        char[] s1 = str1.toCharArray();
        char[] s2 = str2.toCharArray();

        int s1Len = s1.length;
        int s2Len = s2.length;

        int i = 0;//i索引指向s1
        int j = 0;//j索引指向s2

        while (i < s1Len && j < s2Len){//保证匹配时,不越界
            if (s1[i] == s2[j]){//匹配OK
                i++;
                j++;
            } else { //没有匹配成功
                i = i - (j - 1);
                j = 0;
            }
        }

        //判断是否匹配成功
        if (j == s2Len){
            return i - j;
        }else {
            return -1;
        }
    }
}

KMP算法

KMP算法介绍

KMP是一个解决模式串在文本串是否出现过,如果出现过,最早出现的位置的经典算法

KMP方法算法就利用之前判断过信息,通过一个next数组,保存模式串中前后最长公共子序列的长度,每次回溯时,通过next数组找到,前面匹配过的位置,省去了大量的计算时间

参考资料:https://www.cnblogs.com/ZuoAndFutureGirl/p/9028287.html

代码示例:

package com.wxit.kmp;

import java.util.Arrays;

/**
 * @Author wj
 **/
public class KMPAlgorithm {

    public static void main(String[] args) {
        int next[] = kmpNext("ABCDABD");
        System.out.println("next=" + Arrays.toString(next));

        String str1 = "BBC ABCDAB ABCDABCDABDE";
        String str2 = "ABCDABD";

        int index = kmpSearch(str1, str2, next);
        System.out.println(index);
    }

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

    //写出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;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值