hdu1358 循环超过两节的字符串(next数组)

if (i%(i-next[i])==0&&i/(i-next[i])>1)

循环次方=i/(i-next[i])

 1 #include<stdio.h>
 2 #include<string.h>
 3 int n,next[1000005];
 4 char s[1000005];
 5 void getnext()
 6 {
 7   int i,j;
 8   next[1]=j=0;
 9   for (i=2;i<=n;i++)
10   {
11     while (j>0&&s[j+1]!=s[i]) j=next[j];
12     if (s[j+1]==s[i]) j++;
13     next[i]=j;
14   }
15 }
16 int main()
17 {
18   int cnt=0,ans,i;
19   while (~scanf("%d",&n)&&n)
20   {
21     scanf("%s",s+1);
22     printf("Test case #%d\n",++cnt);
23     getnext();
24     ans=0;
25     for (i=2;i<=n;i++)
26       if (i%(i-next[i])==0&&i/(i-next[i])>1)
27         printf("%d %d\n",i,i/(i-next[i]));
28     printf("\n");
29   }
30   return 0;
31 }
View Code

 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1358

posted on 2015-02-01 17:09  xiao_xin  阅读(102)  评论(0编辑  收藏  举报

导航