#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;
}
标签:src,moore,int,des,len,char,find,boyer,size From: https://blog.51cto.com/u_15899033/5902971