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;
}

發佈了53 篇原創文章 · 獲贊 2 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章