#include <iostream>
using namespace std;
int getIndex(const char * p,int next[])
{
    if(p == NULL)
        return -1;
    int k = -1;
    int j = 0;
    next[0] = -1;
    while(p[j])
    {
        if(k==-1||p[k]==p[j])
        {
            ++j;
            ++k;
            if(p[k]!=p[j])
            {
                next[j] = k;
            }
            else
            {
                next[j] = next[k];
            }
        }
        else
        {
            k = next[k];
        }
    }
}


int kmpcompare(const char * srcstr,const char * deststr,int next[] )
{
    if(!srcstr ||!deststr||!next)
        return -1;
    int i = 0;
    int j = 0;
    while(srcstr[i]!='\0'&&deststr[j]!='\0')
    {
        if(srcstr[i] == deststr[j])
        {
            i++;
            j++;
        }
        else
        {
            if(next[j]!=-1)
            {
                j = next[j];
            }
            else
            {
                i++;
                j=0;
            }

        }
    }
    if(deststr[j] == '\0')
    {
        return i-j;
    }
    else
    {
        return -1;
    }

}




Posted on 2008-06-26 17:37 micheal's tech 阅读(196) 评论(0)  编辑 收藏 引用 所属分类: Algorithm

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理