Cod sursa(job #1725169)

Utilizator retrogradLucian Bicsi retrograd Data 5 iulie 2016 07:56:45
Problema Prefix Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.47 kb
#include <fstream>

using namespace std;

char str[2000000];
int PI[2000000];

int main() {
	ifstream fin("prefix.in");
	ofstream fout("prefix.out");

	int t;
	fin >> t;

	while(t--) {
		fin >> (str + 1);

		PI[0] = -1;

		int ans = 0;
		for(int i = 1; str[i]; ++i) {
			int j = PI[i - 1];
			while(j != -1 && str[j + 1] != str[i])
				j = PI[j];
			PI[i] = j + 1;

			if(2 * PI[i] < i) continue;
			if(i % (i - PI[i]) == 0)
				ans = i;
		}

		fout << ans << '\n';
	}
	return 0;
}