#include "stdafx.h"
#include <string>
using namespace std;
void getNextValue(const string& str2,int next[])
{
int i = 0;int j = -1;
next[0] = -1;
while(i<str2.length()-1)
{
if(j==-1||str2[i]==str2[j])
{
++i;++j;
if(str2[i]==str2[j])
next[i] = next[j];
else
next[i] = j;
}
}
}
void getNext(const string& str2,int next[])
{
int i = 0;
next[0] = -1;
int j = -1;
while(i <str2.length()-1)
{
if(j==-1||str2[i]==str2[j])
{
++i;++j;
next[i] = j;
}
else
j = next[j];
}
}
int kmp_index(const string& str1,const string& str2,int next[])
{
int i = 0;int j = 0;
while(i<str1.length()&&j<str2.length())
{
if(j==-1||str1[i]==str2[j])
{
++i;
++j;
}
else
j = next[j];
}
if(j==str2.length())
return i-str2.length();
else
return 0;
}