boyer_moore与find对比测试

256 篇文章 3 订阅
149 篇文章 2 订阅
#include <iostream>
#include <string>
int boyer_moore(const std::string &src, const std::string &des) {
    int size = src.size();
    int len = des.size();
    if (len > size) {
        return -1;
    }
    int i, j;
    i = j = len - 1;
    int num_of_good_char = 0;
    while (i < size && j >= 0) {
        if (src[i] == des[j]) {
            i--;
            j--;
            num_of_good_char++;
            continue;
        }
        if (0 == num_of_good_char) {
            size_t found = des.find_last_of(src[i]);
            if (std::string::npos == found) {
                i += len - 1;
            }
            else {
                i += len - 1 - found;
            }
        }
        else {
            size_t found = des.find_first_of(src[i + num_of_good_char]);
            if (len - 1 == found) {
                i = i + num_of_good_char + len - 1;
                j = len - 1;
            }
            else {
                i = i + len - found - 1;
                j = len - 1;
            } 
            num_of_good_char = 0;
        }
    }
    if (j < 0) {
        return i + 1;
    }
    else {
        return -1;
    }
}
int main() {
    std::string src = "I am a AAX asaaaaaaaKKlXmmkaKKmnnnnnnjnjy8782h+==2aKKnakkLaKKLMMMkkk";
    std::string des = "aKKL";
    for (int i = 0;i < 1000000;i++) {
        //boyer_moore(src, des);      // 0.950s
        src.find(des);                // 0.276s
    }

    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值