POJ 2406 Power Strings

来源:互联网 发布:小米生态链是什么 知乎 编辑:程序博客网 时间:2024/05/19 15:23

Power Strings
Time Limit: 3000MS Memory Limit: 65536KTotal Submissions: 31975 Accepted: 13328

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

abcdaaaaababab.

Sample Output

143





#include<iostream>#include<cstring>using namespace std;char str[1000005];int next[1000005];void getnext(int len){int i,k;i=0;k=-1;next[0]=-1;while(i<=len){if(k==-1 || str[i]==str[k]){i++;k++;next[i]=k;}else    k=next[k];}}int main(){while(cin>>str){if(str[0]=='.')    break;int l=strlen(str);getnext(l);if(2*next[l]<l)cout<<"1\n";else{int mod=l%(l-next[l]);if(mod==0)cout<<l/(l-next[l])<<endl;elsecout<<"1\n";}}return 0;}


0 0