题目链接:http://poj.org/problem?id=2406点击打开链接
Power Strings
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 53824 | Accepted: 22425 |
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
求循环节长度 判断是否可整除即求得个数
#include <stdio.h>
#include <string.h>
using namespace std;
char t[1111111];
int n,m;
int nnext[1111111];
void getnext()
{
int i=0,j=-1;
nnext[0]=-1;
while(i<n)
{
if(j==-1||t[i]==t[j])
{
i++;
j++;
nnext[i]=j;
}
else
{
j=nnext[j];
}
}
}
int main()
{
while(scanf("%s",&t)&&strcmp(t,"."))
{
int ans=1;
n=strlen(t);
getnext();
int mid=n-nnext[n];
//cout << mid << n <<endl;
if((n%mid)==0)
{
ans=(n/mid);
}
printf("%d\n",ans);
}
}