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

Re:晕,KMP写的出错了,谁帮忙看一下,或给组数据测测

Posted by xiaxia at 2006-03-26 23:11:11 on Problem 2752
In Reply To:晕,KMP写的出错了,谁帮忙看一下,或给组数据测测 Posted by:anotherh at 2006-03-26 22:58:23
我的这样
next[0] = -1;
	while(i < len)
	{
		if(j == -1 || S[i] == S[j])
		{		
			++ i;
			++ j;		
			next[i] = j;
		}
		else
			 j = next[j];
	}

> /*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