#include <iostream>
using namespace std;
void GetNext(string StrModel, int* next)
{
int i = 1, j = 0, n = StrModel.length();
next[0] = 0;
while (i < n)
{
if (StrModel[i] == StrModel[j])
{
j++;
next[i] = j;
i++;
}
else
{
if (j > 0)
j = next[j - 1];
else
{
next[i] = j;
i++;
}
}
}
}
void MoveNxet(int** next, int len)
{
for (int i = len - 1; i > 0; i--)
{
(*next)[i] = (*next)[i - 1];
}
(*next)[0] = -1;
}
int StpFind_Kmp(string Src, string StrModel, int curPos = 0)
{
int j = 0, Slen = Src.length() , Mlen = StrModel.length();
int *next = (int*)(_malloca(sizeof(int)* 255));
GetNext(StrModel, next);
MoveNxet(&next, Mlen);
while (curPos <= Slen)
{
if (Src[curPos] == StrModel[j])
{
curPos++;
j++;
}
else
{
curPos++;
j = next[j];
if (j == -1) j++;
}
if (j == StrModel.length())
{
cout << curPos - StrModel.length() << endl;
j = 0;
}
}
_freea(next);
return 0;
}
int main(int argc, int* argv[])
{
printf("hollow world\n");
string src = "qedjabamncjrababcgfqeabcujisjababc";
string modStr = "ababc";
StpFind_Kmp(src, modStr, 0);
cout << ret << endl;
return 0;
}