#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
char a[1000]="abcabaabcacabaabcdefg"; //原串
char b[10]="abaabcac"; //目标串
int next[10]; //存储next值
void nextj(char b[])
{
next[0]=-1;
next[1]=1;
int i=0,j=-1;
while(i<strlen(b))
{
if(j==-1||b[i]==b[j])
{
i++;
j++;
next[i]=j;
}
else
j=next[j];
}
// for(j=0;j<strlen(b);j++)
// cout<<"n"<<j<<"="<<next[j]<<" "<<endl; //输出KMP模式匹配值,便于测试
}
int find(char a[],char b[])
{
int lena=strlen(a);
int lenb=strlen(b);
int i=0,j=0;
while(i<lena)
{
if(a[i]==b[j]||j==-1)
{
j++;
if(j==lenb)
{
return i+1-lenb;
}
}
else
j=next[j];
i++;
}
return -1;
}
int main()
{
nextj(b);
int k=find(a,b);
cout<<"result:"<<k<<endl;
}