(UVA)455 --Periodic Strings(周期串)

题目链接:http://vjudge.net/problem/UVA-455

可以从1开始枚举周期,对后面的字符逐个测试。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     int t;
 9     char s[100];
10     scanf("%d",&t);
11     while(t--)
12     {
13         scanf("%s",s);
14         int len=strlen(s);
15         int loop,test;
16         for(loop=1;loop<=len;loop++)
17         {
18             if(len%loop==0)
19             {
20                 for(test=loop;test<=len;test++)
21                 if(s[test]!=s[test%loop]) break;
22                 if (test==len)
23                 {
24                     printf("%d\n",loop);
25                     break;
26                 }
27             }
28         }
29         if (t) printf("\n");
30     }
31     return 0;
32 }
View Code

 

posted @ 2016-12-03 09:51  ACDoge  阅读(255)  评论(0编辑  收藏  举报