子串判定算法(KMP&字符串哈希)

题目描述

给定字符串haystackneedle,判定needle是否是haystack的子串。以下给出的两种算法复杂度均为O(m)
传送门:Leetcode 28

KMP算法

首先计算needlenext数组,随后执行KMP算法。

class Solution
{
public:
    int strStr(string haystack, string needle)
    {
        if (needle.empty())
            return 0;

        int m = haystack.size();
        int n = needle.size();

        // 'next' array of needle
        vector<int> next(n);
        next[0] = -1;

        int j = -1;
        for (int i = 1; i < n; ++i)
        {
            while (j != -1 && needle[i] != needle[j + 1])
                j = next[j];
            if (needle[i] == needle[j + 1])
                ++j;
            next[i] = j;
        }

        // kmp algorithm
        j = -1;
        for (int i = 0; i < m; ++i)
        {
            while (j != -1 && haystack[i] != needle[j + 1])
                j = next[j];
            if (haystack[i] == needle[j + 1])
                ++j;

            if (j == n - 1)
                return i - n + 1;
        }

        return -1;
    }
};

字符串哈希算法

哈希值 = 26进制字符串

class Solution
{
public:
    int strStr(string haystack, string needle)
    {
        const long long mod = 10e9 + 7;
        int m = haystack.length();
        int n = needle.length();

        if (m < n) return -1;

        long long h_hash = 0;
        long long n_hash = 0;

        long long pow = 1;              // 计算26^n
        
        // 计算原始窗口的哈希值
        for (int i = 0; i < n; ++i)
        {
            pow = (pow * 26) % mod;
            h_hash = (h_hash * 26 + haystack[i] - 'a') % mod;
            n_hash = (n_hash * 26 + needle[i] - 'a') % mod;
        }

        if(h_hash == n_hash) return 0;

        // 移动窗口,动态计算哈希值
        for (int l = 0, r = n; r < m; ++l, ++r)
        {
            h_hash = (h_hash * 26 - pow * (haystack[l] - 'a') + haystack[r] - 'a') % mod;
            if(h_hash == n_hash) return l + 1;
        }

        return -1;
    }
};
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值