【C++】从字符串中匹配字符串

字符串匹配子字符串可以使用以下四种算法:memmem、strstr、sundaysearch函数、KMP算法

1、strstr

首先,我使用最多是strstr函数,若可以匹配到子字符串则返回子字符串的首字母的指针,若不能匹配到则返回NULL;

#include <iostream>
#include <cstring>

using namespace std;

int main(){
        string a = "abcdefg";
        string b = "bcd";
        const char * pos = strstr(a.c_str(), b.c_str());
        if (pos) {
            cout << "找到子串,位置在:" << pos-a.c_str() << endl;
        } else {
            cout << "未找到子串" << endl;
        }
        return 0;
}

结果返回:

此代码中的pos后续不能更改;若想更改,不妨加上 const_cast。

2、memmem

memmem与strstr函数类似

#include <iostream>
#include <cstring>

using namespace std;
int main(){
    string a = "abcdefg";
    string b = "bcd";
    const void* a_ptr = reinterpret_cast<const void*>(a.c_str());
    const void* b_ptr = reinterpret_cast<const void*>(b.c_str());
    auto pos = reinterpret_cast<const char*>(memmem(a_ptr, a.size(), b_ptr, b.size()));
    if (pos) {
        cout << "找到子串,位置在:" << pos - a.c_str() << endl;
    } else {
        cout << "未找到子串" << endl;
    }
    return 0;
}

结果返回

一般情况下 memmem比strstr更快一些。

3、sundaysearch

"SUNDAY 搜索算法"是一种用于字符串匹配的算法。它是根据字符串匹配中常见的"移动窗口"的思想,在搜索过程中尽可能减少比较次数,提高匹配效率。

"SUNDAY 搜索算法"的基本思想如下:

匹配过程:从待搜索的字符串的开头开始,逐个字符与模式串进行匹配。如果当前字符与模式串中对应位置的字符相等,则比较下一个字符;如果不相等,则判断主字符串的下一字节是否在子字符串中出现过:

1、若未出现过,则从主字符串的下一字符开始匹配子字符串;

2、若出现过则,则将子字符串中相同的字符与主字符串相对齐(若子字符串中有相同的字符,则选择最右的字符)。

因此需要一个跳跃表格,记录字符最右出现的位置。

待补充

4、KMP

KMP算法和上述SUNDAY搜索算法类似都是从左到右匹配,并且通过“减枝”以减少不必要的搜索;

KMP算法使用next数组匹配相同的【前缀】和【后缀】进行“减枝”;

通过上述表述,所以KMP算法的核心在于如何求出next数组,并利用数组进行“减枝”;

#include <iostream>
#include <string.h>
#include <vector>
using namespace std;
int GetNext(string str, vector<int> & next) {
        int n = str.size();
        int j = 0;
        next[0] = 0;
        for (int i = 1; i < n; ++i) {
                while (j > 0 && str[i] != str[j]) {
                        j = next[j - 1];
                }
                if (str[i] == str[j]) ++j;
                next[i] = j;


        }

        return 0;
}

int kmp(string a, string b) {
        vector<int> vec(b.size(), 0);
        GetNext(b, vec);
        int a_size = a.size();
        int j = 0;
        for (int i = 0; i < a_size; ++i) {
                while ( j > 0 && a[i] != b[j]) {
                        j = vec[j - 1];
                }
                if (a[i] == b[j]) {
                        ++j;
                }
                if (j == b.size()) {
                        return i - j + 1;
                }
        }
        return -1;
}

int main(){
        string a = "aaaabcabcd";
        string b = "abcd";
        int i = kmp(a, b);
        cout << i << endl;

        return 0;
}

5、Boyer-Moore

评论提到的Boyer-Moore算法,待补充和了解

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值