Cod sursa(job #928292)

Utilizator fhandreiAndrei Hareza fhandrei Data 26 martie 2013 13:15:14
Problema Prefix Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.88 kb
// Include
#include <fstream>
#include <cstring>
using namespace std;

// Constante
const int sz = (int)1e6+3;

// Variabile
ifstream in("prefix.in");
ofstream out("prefix.out");

int tests;
char word[sz];
int prefix[sz];

// Main
int main()
{
	in >> tests;
	while(tests--)
	{
		in >> (word+1);
		int len = strlen(word+1);
		
		int currentPrefix = 0;
		for(int i=2 ; i<=len ; ++i)
		{
			while(currentPrefix && word[i] != word[currentPrefix+1])
				currentPrefix = prefix[currentPrefix];
			
			if(word[i] == word[currentPrefix+1])
				++currentPrefix;
			prefix[i] = currentPrefix;
		}
		
		bool found = false;
		while(len)
		{
			if(prefix[len] && !(prefix[len] % (len-prefix[len])))
			{
				out << len << '\n';
				found = true;
				break;
			}
			--len;
		}
		if(!found)
			out << 0 << '\n';
	}
	
	in.close();
	out.close();
	return 0;
}