這題,求最小周期。有點坑的是格式說明,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