poj2752 Seek the Name, Seek the Fame

http://poj.org/problem?id=2752

水题一枚:next函数应用

View Code
 1 #include<stdio.h>
 2 #include<string.h>
 3 char str1[500005];
 4 int next[500005];
 5 int a[500005];
 6 void get_next(int x)
 7 {
 8     int i=0,j=-1;
 9     next[0]=-1;
10     while(i<x)
11     {
12         if(j==-1||str1[i]==str1[j])
13         {
14             i++;
15             j++;
16             //if(str1[i]!=str1[j])
17             next[i]=j;
18             //else next[i]=next[j];
19         }
20         else j=next[j];
21     }
22 }
23 
24 int main()
25 {
26     while(~scanf("%s",str1))
27     {
28         int len;
29         int len1=strlen(str1);
30         get_next(len1);
31         int k=0;
32         while(len1>0)
33         {
34             a[k++]=len1;
35             len1=next[len1];
36         }
37         for(int i=k-1;i>0;i--)
38         printf("%d ",a[i]);
39         printf("%d\n",a[0]);
40     }
41 }

 

posted on 2012-08-31 16:53  仁者无敌8勇者无惧  阅读(104)  评论(0编辑  收藏  举报

导航