Cod sursa(job #338156)

Utilizator ilincaSorescu Ilinca ilinca Data 5 august 2009 13:32:48
Problema Prefix Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.51 kb
#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;
}