字符串匹配算法BF和KMP总结

本文解析了LeetCode上的一道字符串匹配题目,通过对比暴力求解法与KMP算法,详细介绍了两种方法的实现过程及时间复杂度。暴力求解的时间复杂度为O(mn),而KMP算法的时间复杂度为O(m+n)。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

背景

来看一道leetcode题目:
Implement strStr().

Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.

让我们找到第一个子串的位置,这就是典型的字符串匹配问题。首先想到的就是暴力求解,时间复杂度O(mn).

BF算法实现
class Solution {
public:
    int strStr(string haystack, string needle) {
        if (haystack.size() < needle.size())
            return -1;
        for(int i = 0; i <= haystack.size() - needle.size(); i++) {
            int flag = 1;
            for (int j = 0; j < needle.size(); j++) {
                if (haystack[i + j] != needle[j]) {
                    flag = 0;
                    break;
                }
            }
            if (flag == 1) {
                return i;
            }
        }
        return -1;
    }
};

但是我们可以发现,其实有些字符串我们已经匹配过了,就不需要再次匹配了,这就是经典的KMP算法,时间复杂度为O(m + n)。KMP算法的讲的比较好的,可以参考这个博客:http://www.cnblogs.com/c-cloud/p/3224788.html

KMP算法实现
class Solution {
public:
    int strStr(string haystack, string needle) {
        if (haystack.size() == 0) {
            if (needle.size() == 0)
                return 0;
            else
                return -1;
        } else if (needle.size() == 0)
            return 0;
        if (haystack.size() < needle.size())
            return -1;

        vector<int> Next(needle.size());
        getNext(needle, Next);

        for (int i = 0; i < haystack.size(); ) {
            // 如果剩下的字符串比目标字符串更短,那么直接返回-1.
            if (haystack.size() - i < needle.size())
                return -1;
            int num = 0;
            int step = 1;
            for (int j = 0; j < needle.size(); j++) {
                if (haystack[i + j] == needle[j]) {
                    num++;
                } else
                    break;
            }
            if (num == needle.size())
                return i;
            if (num)
                step = num - Next[num - 1];
            i = i + step;
        }
        return -1;
    }
    void getNext(string needle, vector<int>& next) {
        int k = 0;
        next[0] = 0;
        for (int i = 1; i < needle.size(); i++) {
            while (k > 0 && needle[i] != needle[k])
                k = next[k - 1];
            if (needle[i] == needle[k])
                k++;
            next[i] = k;
        }
    }
};

转载于:https://www.cnblogs.com/zhonghuasong/p/7088703.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值