Cod sursa(job #574616)

Utilizator ChallengeMurtaza Alexandru Challenge Data 7 aprilie 2011 12:48:42
Problema Prefix Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.66 kb
#include <fstream>

using namespace std;

const char InFile[]="prefix.in";
const char OutFile[]="prefix.out";
const int MaxN=1000111;

ifstream fin(InFile);
ofstream fout(OutFile);

int T,n,sol,p[MaxN];
char N[MaxN];

inline void prefix()
{
	n=strlen(N+1);
	sol=0;
	int k=0;
	p[1]=0;
	for(register int i=2;i<=n;++i)
	{
		while(k>0 && N[k+1]!=N[i])
		{
			k=p[k];
		}
		if(N[k+1]==N[i])
		{
			++k;
		}
		p[i]=k;
		if(k!=0)
		{
			if(i%(i-k)==0)
			{
				sol=i;
			}
		}
	}
}

int main()
{
	fin>>T;
	for(register int t=1;t<=T;++t)
	{
		fin>>(N+1);
		prefix();
		fout<<sol<<"\n";
	}
	fin.close();
	fout.close();
	return 0;
}