Pagini recente » Cod sursa (job #2826608) | Cod sursa (job #2591293) | Cod sursa (job #2894852) | Cod sursa (job #209693) | Cod sursa (job #338156)
Cod sursa(job #338156)
#include <stdio.h>
#define nmax 1000005
int n, pi [nmax];
char a [nmax];
int pref ()
{
int i, q=0, r=0;
pi [1]=0;
for (i=2; i < n; ++i)
{
while (q && a [q+1] != a [i])
q=pi [q];
if (a [q+1] == a [i]) ++q;
pi [i]=q;
if (q && i%(i-q) == 0) r=i;
}
return r;
}
int main ()
{
freopen ("prefix.in", "r", stdin);
freopen ("prefix.out", "w", stdout);
int i, t;
scanf ("%d\n", &t);
while (t--)
{
gets (a+1);
for (n=1; a [n]; ++n);
printf ("%d\n", pref ());
}
return 0;
}