C++实现KMP字符串匹配算法

本文详细介绍了KMP算法,包括其原理、构建部分匹配表的过程以及在C++中的实现。KMP算法通过利用已知信息优化字符串匹配,具有时间复杂度O(m+n)的优势。
摘要由CSDN通过智能技术生成

KMP(Knuth-Morris-Pratt)字符串匹配算法是一种用于在一个文本串(text)中查找一个模式串(pattern)的高效算法。它的基本思想是利用已知信息跳过不必要的比较。

KMP 算法的核心是构建一个部分匹配表(Partial Match Table),也称为失配函数(Failure Function)。部分匹配表的作用是记录模式串中每个位置的最长公共前缀和最长公共后缀的长度。通过这个表,可以在匹配过程中根据已匹配的前缀信息来决定模式串的移动位置,从而避免重复比较。

以下是 KMP 算法的主要步骤:

1. 构建部分匹配表:对于模式串中的每个位置,计算该位置的最长公共前缀和最长公共后缀的长度。

2. 匹配过程:在文本串中按照模式串的长度移动,比较文本串和模式串对应位置的字符。当出现失配时,根据部分匹配表的信息来确定模式串应该移动的位置,而不是简单地一位一位地移动。

KMP 算法的时间复杂度为 O(m+n),其中 m 是模式串的长度,n 是文本串的长度。构建部分匹配表的时间复杂度为 O(m),匹配过程的时间复杂度为 O(n)。

KMP 算法在字符串匹配中具有广泛的应用,例如在文本编辑器中的搜索功能、数据压缩中的字符串查找等领域。相比于朴素的字符串匹配算法,KMP 算法能够显著提高匹配效率,尤其是在模式串较长或文本串较长时,优势更加明显。

以下是一个简单的 C++ 程序,演示了 KMP 算法的实现:

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

using namespace std;

// 构建部分匹配表
vector<int> buildPartialMatchTable(const string& pattern) {
    int m = pattern.size();
    vector<int> table(m, 0); // 初始化部分匹配表,默认为0

    int len = 0; // 已匹配的前缀的长度
    int i = 1;

    while (i < m) {
        if (pattern[i] == pattern[len]) {
            len++;
            table[i] = len;
            i++;
        } else {
            if (len != 0) {
                len = table[len - 1]; // 回退到上一个匹配的位置
            } else {
                table[i] = 0;
                i++;
            }
        }
    }

    return table;
}

// KMP 算法实现
vector<int> kmpSearch(const string& text, const string& pattern) {
    int n = text.size();
    int m = pattern.size();
    vector<int> result;

    if (n < m) return result; // 如果文本串长度小于模式串长度,则无需匹配

    vector<int> lps = buildPartialMatchTable(pattern);
    int i = 0; // 文本串中的指针
    int j = 0; // 模式串中的指针

    while (i < n) {
        if (pattern[j] == text[i]) {
            i++;
            j++;
        }

        if (j == m) {
            result.push_back(i - j); // 找到匹配的位置
            j = lps[j - 1]; // 回退到上一个匹配的位置,继续匹配
        } else if (i < n && pattern[j] != text[i]) {
            if (j != 0)
                j = lps[j - 1]; // 回退到上一个匹配的位置
            else
                i++; // 继续匹配文本串中的下一个字符
        }
    }

    return result;
}

int main() {
    string text = "ABABDABACDABABCABAB";
    string pattern = "ABABCABAB";
    
    vector<int> matches = kmpSearch(text, pattern);

    if (matches.empty()) {
        cout << "Pattern not found in text." << endl;
    } else {
        cout << "Pattern found at positions: ";
        for (int pos : matches) {
            cout << pos << " ";
        }
        cout << endl;
    }

    return 0;
}

在这个程序中,buildPartialMatchTable 函数用于构建部分匹配表,kmpSearch 函数实现了 KMP 算法的匹配过程。在 main 函数中,我们演示了如何使用 KMP 算法来在文本串中查找模式串,并输出匹配的位置。

C++标准库中并没有提供类似于KMP算法那样的通用字符串匹配函数。但是,C++标准库中的 <string> 头文件提供了一些基本的字符串操作函数,例如 find()find_first_of(),可以用来进行简单的字符串匹配。

find() 函数:在字符串中查找子串第一次出现的位置,如果找到返回子串第一次出现的位置的索引,否则返回 string::npos

#include <iostream>
#include <string>

int main() {
    std::string str = "hello world";
    std::string substr = "world";
    
    size_t found = str.find(substr);
    if (found != std::string::npos) {
        std::cout << "Substring found at position: " << found << std::endl;
    } else {
        std::cout << "Substring not found." << std::endl;
    }

    return 0;
}

find_first_of() 函数:在字符串中查找给定字符集中的任何字符第一次出现的位置。

#include <iostream>
#include <string>

int main() {
    std::string str = "hello world";
    std::string chars = "aeiou";
    
    size_t found = str.find_first_of(chars);
    if (found != std::string::npos) {
        std::cout << "Vowel found at position: " << found << std::endl;
    } else {
        std::cout << "No vowel found." << std::endl;
    }

    return 0;
}

这些函数可以满足一些简单的字符串匹配需求,但如果需要更复杂的匹配功能(如正则表达式),可能需要使用第三方库(如 Boost 库)或者自行实现。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值