c++Boyer-Moore算法的介绍与代码实现(c++)

Boyer-Moore算法是一种用于字符串匹配的高效算法,主要用于在一个文本串中查找一个模式串的出现位置。它的主要思想是利用模式串中的字符分布信息来尽量减少比较的次数,从而提高匹配的效率。

Boyer-Moore算法的关键思想包括两个部分:坏字符规则(Bad Character Rule)和好后缀规则(Good Suffix Rule)。

  1. 坏字符规则(Bad Character Rule):

    • 当发现不匹配时,根据模式串中当前不匹配的字符(即坏字符),将模式串向右移动一定的位数。
    • 如果模式串中不存在该坏字符,则将模式串整体移动到坏字符后的下一个位置。
  2. 好后缀规则(Good Suffix Rule):

    • 当发现不匹配时,根据模式串中已经匹配的部分(好后缀),将模式串向右移动一定的位数。
    • 如果模式串的好后缀在模式串本身找不到重叠的部分,则尝试将模式串中的后缀与前缀进行匹配,找到可以重叠的相同部分。

Boyer-Moore算法通过不断比较模式串和文本串的字符,利用上述规则来快速定位需要比较的位置,从而减少比较的次数,提高字符串匹配的效率。这种算法在实际应用中效果显著,尤其适用于较长的文本串和模式串。

需要注意的是,Boyer-Moore算法的实现相对复杂,需要考虑各种边界情况和细节,以确保算法的正确性和高效性。

以下是一个简单的 C++ 实现 Boyer-Moore 算法的示例代码:

#include <iostream>
#include <string>
#include <vector>
#include <unordered_map>

int max(int a, int b) {
    return (a > b) ? a : b;
}

void preprocessBadCharShift(const std::string& pattern, std::unordered_map<char, int>& badCharShift) {
    for (int i = 0; i < pattern.length(); i++) {
        badCharShift[pattern[i]] = i;
    }
}

void preprocessGoodSuffixShift(const std::string& pattern, std::vector<int>& goodSuffixShift) {
    int m = pattern.length();
    
    std::vector<int> suffixes(m + 1, 0);
    
    for (int i = 0; i < m; i++) {
        int length = 0;
        while (pattern[i - 1 - length] == pattern[m - 1 - length] && length < i) {
            length++;
            suffixes[length] = i - 1 - length;
        }
    }
    
    for (int i = 0; i <= m; i++) {
        goodSuffixShift[i] = m - suffixes[m];
    }
}

void boyerMooreSearch(const std::string& text, const std::string& pattern) {
    std::unordered_map<char, int> badCharShift;
    std::vector<int> goodSuffixShift(pattern.length() + 1, pattern.length());
    
    preprocessBadCharShift(pattern, badCharShift);
    preprocessGoodSuffixShift(pattern, goodSuffixShift);
    
    int n = text.length();
    int m = pattern.length();
    int s = 0; // Shift of the pattern with respect to the text
    
    while (s <= n - m) {
        int j = m - 1;
        
        while (j >= 0 && pattern[j] == text[s + j]) {
            j--;
        }
        
        if (j < 0) {
            std::cout << "Pattern occurs with shift " << s << std::endl;
            
            s += goodSuffixShift[0];
        } else {
            s += max(goodSuffixShift[j + 1], j - badCharShift[text[s + j]]);
        }
    }
}

int main() {
    std::string text = "AABAACAADAABAABA";
    std::string pattern = "AABA";
    
    boyerMooreSearch(text, pattern);
    
    return 0;
}

这段代码实现了 Boyer-Moore 算法在 C++ 中的基本逻辑。在实际使用中,可以根据具体需求进行定制和优化。请注意,这只是一个简单的示例,实际应用中可能需要考虑更多的情况和细节来确保算法的正确性和高效性。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zhengddzz

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值