升級版的2406,題意:到第i個字符之前,尋找循環節循環的最多次數
Time Limit: 3000MS | Memory Limit: 30000K | |
Total Submissions: 16864 | Accepted: 8108 |
Description
Input
number zero on it.
Output
Sample Input
3 aaa 12 aabaabaabaab 0
Sample Output
Test case #1 2 2 3 3 Test case #2 2 2 6 2 9 3 12 4
Source
#include<stdio.h>
#include<string.h>
int next[1123456];
char str[1123456];
int n;
void get_next()
{
next[0]=-1;
int i=0;
int j=-1;
while(i<n)
{
if(j==-1 || str[i]==str[j])
{
i++;
j++;
next[i]=j;
}
else
{
j=next[j];
}
}
}
int main()
{
int t=1;
while(~scanf("%d",&n) && n)
{
scanf("%s",str);
get_next();
if(t>1)
printf("\n");
printf("Test case #%d\n",t++);
for(int i=2;i<=n;i++)
{
if(i%(i-next[i])==0 && i/(i-next[i])!=1)
{
printf("%d %d\n",i,i/(i-next[i]));
}
}
}
return 0;
}