c++数据结构KMP算法

来源:百度知道 编辑:UC知道 时间:2024/06/30 13:49:48
谁可以帮我编写个关于C++KMP算法的实现程序,,我实在是看书看不下去了..自己实验好几次都不能实现这个算法..请各位前辈帮个忙吧..
那个可不可以再简单化点呀..我还没有学到重造构造器呀..
可以的VC++6.0上编译的程序呀

代码如下:
#include <vector>
#include <string>
#include <iostream>
using namespace std;
const vector<int> * kmp_next(string &m) // count the longest prefex string ;
{
static vector<int> next(m.size());
next[0]=0; // the initialization of the next[0];

int temp; // the key iterator......

for(int i=1;i<next.size();i++)
{
temp=next[i-1];

while(m[i]!=m[temp]&&temp>0)
{ temp=next[temp-1];
}

if(m[i]==m[temp])
next[i]=temp+1;
else next[i]=0;

}

return &next;

}

bool kmp_search(string text,string m,int &pos)
{
const vector<int> * next=kmp_next(m);

int tp=0;
int mp=0; // text pointer and match string pointer;

for(tp=0;tp<text.size();tp++)
{
while(text[tp]!=m[mp]&&mp)
mp=(*next)[mp-1];

if(text[tp]==m[mp])
mp