joj 1873 Power Strings

 1873: Power Strings


ResultTIME LimitMEMORY LimitRun TimesAC TimesJUDGE
10s 8192K 650 258 Standard
Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b = "abcdef". If we think of concatenation as multiplication, exponentiation by a non-negative integer is defined in the normal way: a^0 = "" (the empty string) and a^(n+1) = a*(a^n).

Each test case is a line of input representing s, a string of printable characters. For each s you should print the largest n such that s = a^n for some string a. The length of s will be at least 1 and will not exceed 1 million characters. A line containing a period follows the last test case.

Sample Input

abcd
aaaa
ababab
.

Output for Sample Input

1
4
3

 


This problem is used for contest: 73 


Submit / Problem List / Status / Discuss

 
一个基础的周期串问题。
有更好的办法,不过还是很黄很暴力的枚举吧(大爱各种暴力流水流)...
不过这样写真心跑得很慢...
 
 1 #include <stdio.h>
 2 #include <string.h>
 3 
 4 int main()
 5 {
 6     char s[1000005];
 7     int n;
 8     int i, j;
 9     
10     //freopen("in.txt", "r", stdin);
11     
12     while (scanf("%s", s) == 1, s[0] != '.')
13     {
14         n = strlen(s);
15         
16         for (i=1; i<=n; ++i)
17         {
18             if (0 == n % i)
19             {
20                 int ok = 1;
21                 for (j=i; j<n; ++j)
22                 {
23                     if (s[j] != s[j%i])
24                     {
25                         ok = 0;
26                         break;
27                     }
28                 }
29                 if (ok)
30                 {
31                     printf("%d\n", n/i);
32                     break;
33                 }
34             }
35         }
36     }
37     
38     
39     return 0;
40 }

 

 
posted @ 2012-05-03 17:27  漂木  阅读(173)  评论(0编辑  收藏  举报