ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2021-05-09 16:02:09  阅读:161  来源: 互联网

标签:charAt int str2 str1 dest 算法 KMP 字符串 String


一、暴力匹配

package com.monster.kmp;

/**
 * @author Monster
 * @version v1.0
 * @time 05-09-2021 14:06:06
 * @description:
 */
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[] chars1 = str1.toCharArray();
        char[] chars2 = str2.toCharArray();

        int i = 0; // 用于记录数组 chars1 的下标
        int j = 0; // 用于记录数组 chars2 的下标

        while (i < str1.length() && j < str2.length()) {  // 匹配到一个就停止
            if (chars1[i] == chars2[j]) {
                i++;
                j++;
            }else {
                i = i - j + 1;
                j = 0;
            }
        }

        if(j == str2.length()) {
            return i - j;
        }else {
            return -1;
        }
    }
}

二、kmp算法

package com.monster.kmp;

import java.util.Arrays;

/**
 * @author Monster
 * @version v1.0
 * @time 05-09-2021 15:11:52
 * @description:
 */
public class KMPAlgorithm {
    public static void main(String[] args) {
        String str1 = "BBC ABCDAB ABCDABCDABDE";
        String str2 = "ABCDABD";
        int[] kmpNext = kmpNext(str2);
        System.out.println("kmpNext = " + Arrays.toString(kmpNext));
        int search = kmpSearch(str1, str2, kmpNext);
        System.out.println("search = " + search);
    }

    /**
     *
     * @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) {

        int[] next = new int[dest.length()];
        next[0] = 0; // 当字符串的长度为 1时部分匹配值就是 0

        // i表示字符串的索引,j 表示部分匹配值,
        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];
            }
            // 当满足条件时,部分匹配值 +1
            if(dest.charAt(i) == dest.charAt(j)) {
                j++;
            }
            next[i] = j;
        }
        return next;
    }
}

标签:charAt,int,str2,str1,dest,算法,KMP,字符串,String
来源: https://blog.csdn.net/qwertyu0103/article/details/116565861

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有