sunday 最快的字符串匹配

来源:互联网 发布:人工智能无人驾驶股票 编辑:程序博客网 时间:2024/05/16 19:43
#include<iostream>
#include<string>
using namespace std;


int sunday(const char *text, const char *find)
{

char map[CHAR_MAX];
int i;
int text_len = strlen(text);
int find_len = strlen(find);
if (text_len < find_len)
return -1;
//preprocess  
for (i = 0; i<CHAR_MAX; i++)
map[i] = find_len + 1;
for (i = 0; i<find_len; i++)
map[find[i]] = find_len - i;
//match process  
i = 0;
while (i <= (text_len - find_len))
{
if (strncmp(find, text + i, find_len) == 0)
return i;
else
i += map[text[i + find_len]];
}
return -1;
}
int main(){
char*value = "you are my pretty";
char *cmp = "ty";
cout << sunday(value,cmp);
}