SDUTOJ 2476Period

#include<iostream>
#include<string.h>
#include<stdio.h>
#define N 1000010
using namespace std;
char s[N];
int next[N];
void getnext(char s[])
{
	int j=-1,i=0,len;
	next[0]=-1;
        len=strlen(s);
	while(i<=len)
	{
		if(j==-1||s[i]==s[j])
		{
			++i;
			++j;
			next[i]=j;
		}
		else
		    j=next[j];
	}
}
int main()
{
	int j=1,i,n;
	while(cin>>n)
	{
		if(n==0)
		{
			break;
		}
		else
		cin>>s;
		getnext(s);
		cout<<"Test case #"<<j<<endl;
		j++;
		for(i=2;i<=n;i++)
		{
			if(i%(i-next[i])==0 && i!=i-next[i])
				cout<<i<<" "<<(i/(i-next[i]))<<endl;
		}
		cout<<"\n";
	}
	return 0;
}

posted @ 2017-05-23 08:52  yfceshi  阅读(220)  评论(0编辑  收藏  举报