Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

非本题代码!kmp的next求法还是有点讲究的

Posted by xbmmp at 2020-04-13 21:21:36 on Problem 2406
void nextvalue()
{
    int plen = P.length();
    int j = 0;
    int k = -1;
    next[0] = -1;
    while(j < plen - 1)
    {
        if(k == -1 || P[j] == P[k])
        {
            j++;
            k++;
            if(P[j] == P[k])
                next[j] = next[k];//和下面注释都是关键
            else
                next[j] = k;//还有这里
        }
        else
            k = next[k];
    }
}
int kmp()
{
    int i,j;
    i = j = 0;
    int plen = P.length();
    int slen = S.length();
    while(i < slen && j < plen)
    {
        if(j == -1 || S[i] == P[j])
        {
            i++;
            j++;
        }
        else
            j = next[j];
    }
    if(j == plen)
        return i - j;
    else
        return -1;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator