UVa455 - Periodic Strings

点击打开链接

小心最后一行输出后换行别空行。

#include<stdio.h>
#include<string.h>
char s[85];
int main(){
	int m, n, i = 1, k = 0;
	scanf("%d", &m);
	while (m--)
	{
		int ok, ok1 = 0;
		memset(s, '\0', sizeof(s));
		scanf("%s", s);
		n = strlen(s);
		if (n == 1)									//串形如“A”
		{
			printf("%d", n);
			if (m) printf("\n\n");
			else printf("\n");
			continue;
		}
		for (int j = 1; j < n; j++){				//串形如“ABCABCABC”
			if (n % j == 0){
				for (k = 0; k < n - j; k++){
					if (s[k] != s[k + j]) {
						ok = 0; break;
					}
					else ok = 1;
				}
			}
			if (ok){
				printf("%d", j);
				if (m) printf("\n\n");
				else printf("\n");
				ok = 0;
				ok1 = 1;
				break;
			}
		}
		if (!ok1){										//串形如“ASDFGH”
			for (int q = 1; q < n; q++){
				if (s[0] == s[q]){
					for (int w = 0; w < n; w++)
					{
						if (s[w + q] != s[w]) break;
					}
				}
				if (q == n - 1 && !ok1){
					printf("%d", n);
					if (m) printf("\n\n");
					else printf("\n");
				}
			}
		}
	}
	return 0;
}

你可能感兴趣的:(UVa455 - Periodic Strings)