| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
Re:晕,KMP写的出错了,谁帮忙看一下,或给组数据测测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: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator