国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

POJ 1961 Period

2019-11-06 06:33:53
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

Description

For each PRefix of a given string S with N characters (each character has an ASCII code between 97 and 126, inclusive), we want to know whether the prefix is a periodic string. That is, for each i (2 <= i <= N) we want to know the largest K > 1 (if there is one) such that the prefix of S with length i can be written as AK ,that is A concatenated K times, for some string A. Of course, we also want to know the period K.

Input

The input consists of several test cases. Each test case consists of two lines. The first one contains N (2 <= N <= 1 000 000) – the size of the string S.The second line contains the string S. The input file ends with a line, having the number zero on it.

Output

For each test case, output "Test case #" and the consecutive test case number on a single line; then, for each prefix with length i that has a period K > 1, output the prefix size i and the period K separated by a single space; the prefix sizes must be in increasing order. Print a blank line after each test case.

Sample Input

3aaa12aabaabaabaab0

Sample Output

Test case #12 23 3Test case #22 26 29 312 4

Source

Southeastern Europe 2004

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

KMP~

求出next[i],然后枚舉截止位置i,則循環(huán)節(jié)的長(zhǎng)度now=i-next[i],如果now!=i,則表明循環(huán)了不止一次,如果同時(shí)i%now==0(即完整循環(huán)),直接輸出即可~

#include<cstdio>#include<cstring>int len,next[1000001],k,totcase,now;char s[1000001];int main(){	while(scanf("%d",&len)==1 && len)	{		scanf("%s",s+1);k=0;		printf("Test case #%d/n",++totcase);		for(int i=2;i<=len;i++)		{			while(k && s[i]!=s[k+1]) k=next[k];			if(s[i]==s[k+1]) k++;next[i]=k;		}		for(int i=1;i<=len;i++)		{			now=i-next[i];			if(now!=i && !(i%now)) printf("%d %d/n",i,i/now);		}		printf("/n");	}	return 0;}


發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 栖霞市| 遵义县| 南阳市| 通城县| 兴仁县| 扶沟县| 富源县| 庆安县| 靖边县| 千阳县| 云林县| 肥乡县| 屯门区| 定边县| 昭平县| 滕州市| 锡林浩特市| 邵东县| 准格尔旗| 汾西县| 高陵县| 基隆市| 宝丰县| 黔西县| 文山县| 兴文县| 祥云县| 定西市| 柳林县| 紫云| 峨边| 平乐县| 淅川县| 车险| 乌苏市| 长沙县| 贵德县| 鄯善县| 吉水县| 九江县| 丰都县|