GitBook

Periodic Strings
Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu

Description


 Periodic Strings 

A character string is said to have period k if it can be formed by concatenating one or more repetitions of another string of length k. For example, the string "abcabcabcabc" has period 3, since it is formed by 4 repetitions of the string "abc". It also has periods 6 (two repetitions of "abcabc") and 12 (one repetition of "abcabcabcabc").

Write a program to read a character string and determine its smallest period.

Input

The first line oif the input file will contain a single integer N indicating how many test case that your program will test followed by a blank line. Each test case will contain a single character string of up to 80 non-blank characters. Two consecutive input will separated by a blank line.

Output

An integer denoting the smallest period of the input string for each input. Two consecutive output are separated by a blank line.

Sample Input

1

HoHoHo

Sample Output

2


题目大意就是在一个字符串中找到它是由长度多少的小字符串重复而形成的

我已经感受到了 紫书习题满满的恶意

本来以为思路都全部写出来之后 写代码只是体力劳动

结果……呵呵哒

这一题也是费了挺大的劲的 -.-

以至于不愿意回忆

具体的都写在代码的注释里了



#include<stdio.h>
#include<string.h>
int main() {
	int T,l;
	char s[100];
	while(scanf("%d",&T)!=EOF) {
//	(去掉也行)	getchar();
// Two consecutive input will separated by a blank line.
		while(T--) {
//	(去掉也行)	getchar();
// 两相邻的输入之间有一个空行 -.-
			memset(s,0,sizeof(s));
			scanf("%s",s);
			l=strlen(s);
			for(int j,i=1; i<=l; i++) {
				//刚开始因为i<=l写成i<l而一直WA
				//可见题目数据给的确实是很有水平啊
				if (l%i==0) {
//				for(int j=0; j<i; j++) {
//					for(int k=0; k<l-i; k+=i) {
//						if(s[j+k]!=s[j+k+i]) {
//	本来想用这种思路 但是仔细想 写起来太麻烦 所以换了一种
//						}
//					}
//				}
					for(j=i; j<l; j++) {
						if(s[j%i]!=s[j]) {
							break;
						}
					}
					if(j==l) {
						printf("%d\n",i);
						break;
					}
				}
			}
			if(T)
				printf("\n");
		}
	}
	return 0;
}


题目地址:【UVa】[455]Periodic Strings