Cod sursa(job #1405130)

Utilizator mircea.dobreanuMircea Dobreanu mircea.dobreanu Data 28 martie 2015 21:19:42
Problema Prefix Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.82 kb
#include <fstream>
#include <string>
#include <vector>
using namespace std;

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

inline void prefixFunction(vector<int>& pi, string& s)
{
	int n = s.size();
	pi.assign(n, 0);

	for (int i = 1; i < n; ++i)
	{
		int j = pi[i - 1];

        while (j > 0 && s[i] != s[j])
			j = pi[j - 1];
		if (s[i] == s[j])
			++j;
		pi[i] = j;
	}
}

int main()
{
	ios_base::sync_with_stdio(false);
	int t = 0;	fin >> t;


	string s;
	vector<int> pi;
	pi.reserve(1e6 + 1);

	while (t--)
	{
		fin >> s;
		prefixFunction(pi, s);

        int res = 0;
        for (int i = pi.size(); i > 0; --i)
		{
			if (pi[i - 1] && i % (i - pi[i - 1]) == 0)
			{
				res = i;
                break;
			}
		}
		fout << res << '\n';

		pi.clear();
	}
	return 0;
}