Time Limit: 3000MS | Memory Limit: 30000K | |
Total Submissions: 16765 | Accepted: 8069 |
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
題意:求每個點是否存在循環節,
分析:求出next[ ]數組,如果 i%(i-next[i])==0 && next[i]!=0則有循環節次數i/(i-next[i]) ,循環節長度i-next[i]
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
using namespace std;
#define maxx 1123456
int next[maxx];
char t[maxx];
void get_next(char t[])
{
int l=strlen(t);
int i=0,j=-1;
next[0]=-1;
while(i<l)
{
if(j==-1 || t[i]==t[j])
{
i++;j++;next[i]=j;
}
else
{
j=next[j];
}
}
}
int main()
{
int n,m,i,j;
int ll=1;
while(scanf("%d",&n)&&n)
{
scanf("%s",t);
get_next(t);
int l=n;
printf("Test case #%d\n",ll++);
for(i=2;i<=l;i++)
{
if(i%(i-next[i])==0 && next[i]!=0)
printf("%d %d\n",i,i/(i-next[i]));
}
printf("\n");
}
return 0;
}