Period UVA - 1328_结论题

 

Code:

#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=1000000+5;
char S[maxn];
int f[maxn];
void getfail(char p[],int n)
{
	f[0]=0,f[1]=0;
	for(int i=1;i<n;++i)
	{
		int j=f[i];
		while(j&&p[i]!=p[j])j=f[j];
		f[i+1]=p[i]==p[j]?j+1:0;
		if(f[i+1]!=0&&(i+1)%(i+1-f[i+1])==0)
		{
				printf("%d ",i+1);
				printf("%d\n",(i+1)/(i+1-f[i+1]));
		}
	}
}
int main()
{
	int cnt=0;
    while(1)
    {
        int n;scanf("%d",&n);if(!n)return 0;
    	printf("Test case #%d\n",++cnt);
    	scanf("%s",S);
        getfail(S,n);
        printf("\n");
    }
    return 0;
}

  

posted @ 2018-09-19 21:01  EM-LGH  阅读(172)  评论(0编辑  收藏  举报