这题,求最小周期。有点坑的是格式说明,Two
consecutive output are separated by a blank line.两种数据输出之间要有个空白行。
还有另外一个,len % t == 0是必须得,因为只有是周期倍数的才能求出周期。不然如:abcdabc求出的周期就变成了3.
1 #include <stdio.h>
2 #include <string.h>
3 const int maxn = 1024;
4 char buf[maxn] = {0};
5 int isT(char * buf, int len,int t);
6 int main() {
7
8 int n;
9 while (~scanf("%d",&n))
10 while ( n > 0 ) {
11
12 // 输入
13 scanf("%s",buf);
14 int len = strlen(buf);
15
16 // 计算最小周期,从小到大枚举
17 for (int t = 1; t <= len; t++) {
18 if (len % t == 0)
19 if (isT(buf, len, t)) {
20 printf("%d\n", t);
21 break;
22 }
23 }
24
25 if (--n) printf("\n");
26 }
27
28 return 0;
29 }
30
31 int isT(char * buf, int len,int t) {
32
33 for (int i = t; i < len; i++) {
34
35 if (buf[i % t] != buf[i]) {
36
37 return 0;
38 }
39 }
40
41 return 1;
42 }
by sixleaves