c++ 字符串匹配算法Robin-Karp算法

在看 indexRabinKarp 函数之前,我们先了解一下 RabinKarp 算法。

RobinKarp算法是由 Robin 和 Karp 提出的字符串匹配算法。该算法在实际应用中有较好的表现。

算法的核心步骤:

  • const primeRK = 16777619 // 大素数
  • 对 substr 构造 hash 值。 n = len(substr),hash = (substr[0]*pow(primeRK, n-1) + substr[1]*pow(primeRK, n-2) + … + substr[n-1]*pow(primeRK, 0)) % anotherBiggerPrime
  • 对 s 的每 n 个子串按照相同逻辑构造 hash 值,判断与 substr 的 hash 是否相等;如果 hash 相等,则比较子串是否真的与 substr 相等
    重复第三步,直到找到,或者未找到。

ps:

  • 该算法之所以快,是因为 s[i+1, i+n+1] 的 hash 值可以由 s[i, i+n] 的 hash 值计算出。即h(i+1) = ((h(i) - s[i] * pow(primeRK, n-1)) * primeRK + s[i+n+1]) % anotherBiggerPrime
  • 另外,go 计算 hash 时并没有 % anotherBiggerPrime,而是定义了 hash 为 uint32 类型,利用整型溢出实现了对 2**32 取模的效果。(一般来说是对另一个大素数取模,显然这里不是,不过毕竟这么大的数也没啥大影响)

该算法预处理时间为 O(n),n 为 len(substr),运行最坏时间为 O((n-m+1)m),m 为 len(s)。最坏情况为每个子串的 hash 都与 substr 的一样。在平均情况下,运行时间还是很好的。

除了 RabinKarp 算法外,还要一些其他的字符串匹配算法。《算法》导论中介绍了另外两种优秀的算法,分别是 有限自动机 与 Knuth-Morris-Pratt 算法(即 KMP 算法),这两个算法的运行时间都为 O(m)。
下面是 indexRabinKarp 函数

#include <iostream>
#include <utility>
#include <string>
#include <cstring>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <numeric>

#include <fstream>

using namespace std;

constexpr unsigned int primeRK = 16777619;

unsigned int quickMi(unsigned int base, int n) {
    unsigned int ans = 1;
    while (n) {
        if (n & 0x1) {
            ans *= base;
        }
        base *= base;
        n >>= 1;
    }
    return ans;
}

void hashStr(const string &s, unsigned int &hash) {
    hash = 0;
    for (auto c : s) {
        hash = (hash * primeRK) + c;
    }
    return;
}

bool strcmp(const char *source, const char *target, int p1, int p2, int n) {
    for (int i = 0; i < n; i++) {
        if (source[p1++] != target[p2++]) {
            return false;
        }
    }
    return true;
}

int indexRabinKarp(const string &source, const string &target) {
    if (source.size() < target.size()) {
        return -1;
    }
    int n = target.size();
    unsigned int tHash, p;
    hashStr(target, tHash);
    p = quickMi(primeRK, target.size());

    unsigned int h;
    hashStr(source.substr(0, target.size()), h);
    if (tHash == h && strcmp(source.c_str(), target.c_str(), 0, 0, n)) {
        return 0;
    }
    int i = 0;
    while (i + n < source.size()) {
        h = h * primeRK + source[i + n];
        h -= (p * source[i]);
        i++;
        if (tHash == h && strcmp(source.c_str(), target.c_str(), i, 0, n)) {
            return i;
        }
    }
    return -1;
}

int main() {
    cout << indexRabinKarp("aabcdef", "bcdef") << endl;
}

实战

https://leetcode-cn.com/problems/implement-strstr/
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值