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. 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.
For each s you should print the largest n such that s = a^n for some string a.
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std;
int pext[1000000] = {-5};
char s[1000000];
int len1;
void get_next(char *T,int *next)
{
int k = -1;
int j = 0;
pext[j] = k;
while(j < len1)
{
if((k == -1) || (T[j] == T[k]))
{
k++;
j++;
pext[j] = k;
}
else
{
k = pext[k];
}
}
}
int main()
{
while(1)
{
scanf("%s",s);
if(strcmp(s,".") == 0)
{
return 0;
}
else
{
int sum = 0;
len1 = strlen(s);
get_next(s,pext);
int z;
z = len1 - pext[len1];
if(len1 % z ==0)
{
for(int i=1;i<=len1;i++)
{
if(i%z==0)
sum++;
}
printf("%d\n",sum);
}
else
{
printf("1\n");
}
}
}
return 0;
}