#include <iostream>
#include <functional>
#include <string.h>
std::function< int(char *, char *) > find = [](char *str, char *p) {
int * next = new int[strlen(p)], len = strlen(p);
next[0] = -1;
int i = 0, j = -1;
while (i < len)
{
if (j == -1 || p[i] == p[j]) {
next[++i] = ++j;
}
else
j = next[j];
}
i = 0, j = 0;
int len1 = strlen(str);
while (i < len1)
{
if (str[i] == p[j])
i++, j++;
else if (j == -1)
j = 0, i++;
else
j = next[j];
if (j == len - 1)
break;
}
return j == len - 1 ? i - j : -1;
};
int main()
{
char p[] = { "ihabababcakhsd" };
char p1[] = { "abababca" };
std::cout << find(p, p1) << std::endl;
}