BM算法 还没理解,先放着
#include <iostream>
//找到一个子字符串
using namespace std;
int bm_substr(char *str, char *sub)
{
int len = strlen(sub);
int aux[256] = {0};
for(int i = 0; i < len; i++)
{
aux[str[i]] = i;
}
int n = strlen(str);
int i = len-1;
while( i < n)
{
int j = i, k = len-1;
while( k >= 0 && str[j--] == sub[k--]);
if( k < 0)return j+1;
if( i+1 < n)
{
i+=aux[str[i+1]];
}
else
return -1;
}
}
int main()
{
cout << bm_substr("hello world","world")<<endl;
return 0;
}