码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 2406 Power Strings

时间:2015-08-07 18:53:14      阅读:83      评论:0      收藏:0      [点我收藏+]

标签:

Power Strings

 

Description

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).

Input

Each test case is a line of input representing s, a string of printable characters. 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.

Output

For each s you should print the largest n such that s = a^n for some string a.

Sample Input

abcd
aaaa
ababab
.

Sample Output

1
4
3

Hint

This problem has huge input, use scanf instead of cin to avoid time limit exceed.
 
根据next数组的特性,如果字符串里面的某个子串开始循环出现,那么next[i]会从1开始自然数式增长,而且循环是从最后一个next[i]=1处开始,直到结束,len-next[len]就等于循环子串的长度,这道题还得考虑这个子串最后一次是否完整的循环。
 
 1 #include<cstdio>
 2 #include<cstring>
 3 using namespace std;
 4 
 5 const int maxn=1000005;
 6 
 7 int next[maxn];
 8 
 9 void getnext(char str[])
10 {
11     int i,j;
12     int len=strlen(str);
13     i=0;j=-1;
14     next[i]=j;
15     while(i<=len)
16     {
17         if(j==-1||str[i]==str[j])
18         {
19             i++;
20             j++;
21             next[i]=j;
22         }
23         else
24         j=next[j];
25     }
26 }
27 
28 int main()
29 {
30     //freopen("in.txt","r",stdin);
31     int cnt,i;
32     char s[maxn];
33     while(scanf("%s",s)&&strcmp(s,".")!=0)
34     {
35         getnext(s);
36         int len=strlen(s);
37         if(len%(len-next[len])==0)
38         printf("%d\n",len/(len-next[len]));
39         else
40         printf("%d\n",1);
41     }
42     return 0;
43 }

 

 

POJ 2406 Power Strings

标签:

原文地址:http://www.cnblogs.com/homura/p/4711084.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!