【UVa】[455]Periodic Strings

Problem Description

Loading...

Problem Analysis

The gist of the problem is to find, within a string, the length of the small string whose repetition produces it.

I can already feel the sheer malice of the exercises in the Purple Book.

I had thought that once the whole approach was written out, coding would be mere manual labor.

Turns out… heh.

This one also cost me quite a bit of effort -.-

So much so that I don’t want to recall it.

The details are all written in the code comments.

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#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;
}

Problem link: 【UVa】[455]Periodic Strings

Loading...