#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);
}
#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);
}