A

来源:互联网 发布:硬笔字帖软件 编辑:程序博客网 时间:2024/06/10 03:59

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
Hint
This problem has huge input, use scanf instead of cin to avoid time limit exceed.


题意:找到一个最短的循环节,输出它的循环次数。。。

思路:用字符串长度减去最后一个字符匹配的最长的前后缀匹配长度即是最短循环节的长度,再用总长度相除即可,没有最小循环节的输出1.。。。

下面附上代码:

#include<cstdio>#include<cstring>#include<algorithm>const int N=1000005;using namespace std;int Next[N];char s[N];void Getnext(char *t,int n){int i=0,j;Next[i]=-1,j=Next[i];while(i<n){if(j==-1||t[i]==t[j])Next[++i]=++j;else j=Next[j];}}int main(){while(~scanf("%s",s)){if(s[0]=='.') break;int l=strlen(s);Getnext(s,l);if(l%(l-Next[l])==0) printf("%d\n",l/(l-Next[l]));else puts("1");}return 0;}