// exam1.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
using namespace std;
void get_next(int* &next,char* s)
{
int j=0;
int i=1;
int len=strlen(s);
next=(int*)malloc(len*sizeof(int));
memset(next,0,len*sizeof(int));
while(s[i]!='\0')
{
if(s[j]==s[i])
{
i++;
j++;
if(s[i]!=s[j])
{
next[i]=j;
}
else
{
next[i]=next[j];
}
}
else
{
if(j==0)
{
i++;
}
j=next[j];
}
}
}
int kmp(int* next,char* s,char* m)
{
int i=0,j=0;
while(s[i]!='\0')
{
if(m[j]=='\0')
{
break;
}
if(s[i]==m[j])
{
i++;
j++;
}
else
{
if(j==0)
{
i++;
}
j=next[j];
}
}
return i-strlen(m);
}
int main(void)
{
int len,*next;
char *s="acabaabaabcacaabc";
char *m="abaabcac";
len=strlen(m);
get_next(next,m);
int loc=kmp(next,s,m);
/*for(int i=0;i<len;i++)
{
cout<<next[i]<<endl;
}*/
cout<<loc<<endl;
system("pause");
return 0;
}
code2
#include "stdafx.h"
#include <iostream>
#include <string>
#include <stack>
using namespace std;
void getNext(char* s,int* &next)
{
<span style="white-space:pre"> </span>next=new int[strlen(s)];
<span style="white-space:pre"> </span>memset(next,0,strlen(s)*sizeof(int));
<span style="white-space:pre"> </span>int i=0;
<span style="white-space:pre"> </span>int j=-1;
<span style="white-space:pre"> </span>next[0]=-1;
<span style="white-space:pre"> </span>while(i<strlen(s))
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>if(j==-1 || s[i]==s[j])
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>i++;
<span style="white-space:pre"> </span>j++;
<span style="white-space:pre"> </span>if(s[i]==s[j])
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>next[i]=next[j];
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>else
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>next[i]=j;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>else
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>j=next[j];
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
}
void kmp(char* s,char* t,int* next)
{
<span style="white-space:pre"> </span>int i=0;
<span style="white-space:pre"> </span>int j=0;
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>int len1=strlen(s);
<span style="white-space:pre"> </span>int len2=strlen(t);
<span style="white-space:pre"> </span>while(i<len1 && j<len2)
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>if(j==-1 || s[i]==t[j])
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>i++;
<span style="white-space:pre"> </span>j++;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>else
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>j=next[j];
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>if(j==strlen(t))
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>cout<<s+i-strlen(t)<<endl;
<span style="white-space:pre"> </span>}
}
int main(void)
{
<span style="white-space:pre"> </span>char* s="acabaabaabcacaabc";
<span style="white-space:pre"> </span>char* t="abaabcac";
<span style="white-space:pre"> </span>int* next;
<span style="white-space:pre"> </span>getNext(t,next);
<span style="white-space:pre"> </span>kmp(s,t,next);
<span style="white-space:pre"> </span>system("pause");
<span style="white-space:pre"> </span>return 0;
}