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写的出错了,谁帮忙看一下,或给组数据测测

Posted by anotherh at 2006-03-26 22:58:23 on Problem 2752
/*pku 2752*/
#include<stdio.h>
#define N 400005
char s[N],next[N],out[N],n,ans;

int kmp()
{
    int ii,p;
    p=-1,next[0]=-1;
    for(ii=1;s[ii]!='\0';ii++)
    {
        while(p!=-1&&s[ii]!=s[p+1])
            p=next[p];
        if(s[ii]==s[p+1])
            p++;
        next[ii]=p;
    }
    return ii;/*字符串长度*/
}

int main()
{
    int n;
    while(scanf("%s",s)!=EOF)
    {
        n=kmp()-1;
        ans=0;
        out[ans++]=n+1;/*长度用数组存起来*/
        while(next[n]!=-1)
        {
            num++;
            n=next[n];
            out[ans++]=n+1;
        }
            
        printf("%d",out[--ans]);
        while(ans--)    
           printf(" %d",out[ans]);
        
        putchar('\n');
    }
}

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