在网上发现一篇关于kmp的nice解说,所以就把他的思路分享给大家 下面是原作者的博客 点击打开链接。
再分享下我的关于求next[]部分匹配值的我自己的代码
#include<iostream>
#include<string.h>
using namespace std;
void sort( char a[],int next[])
{
int x = 0;
for (int i = 1; i < strlen(a); i++)
{
while (x > 0 && a[i] != a[x])
{
x = next[x - 1];
}
if (a[i] == a[x])
{
x++;
}
next[i] = x;
}
}
void main()
{
char a[] = "abcdabd";
int next[20] = { 0 };
sort(a, next);
for (int i = 0; i < strlen(a);i++)
{
cout << next[i] << " ";
}
system("pause");
}