Cod sursa(job #2458263)

Utilizator ardutgamerAndrei Bancila ardutgamer Data 20 septembrie 2019 00:41:15
Problema Prefix Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.81 kb
#include <iostream>
#include <fstream>
#include <cstring>
#include <string>

using namespace std;

const int NMAX = 1000005;
int phi[NMAX];
string s;

void KermitManancaPrune(string s)
{
	memset(phi, 0, sizeof(phi));
	phi[0] = -1;
	int n = s.size();
	for (int i = 1; i < n; i++)
	{
		int x = i - 1;
		while (s[phi[x] + 1] != s[i] && phi[x] != -1)
			x = phi[x];
		if (s[phi[x] + 1] == s[i])
			phi[i] = phi[x] + 1;
		else
			phi[i] = -1;
	}
}

int main()
{
	ifstream cin("prefix.in");
	ofstream cout("prefix.out");
	int t;
	cin >> t;
	while (t--)
	{
		cin >> s;
		KermitManancaPrune(s);
		int answer = 0;
		for (int i = s.size() - 1; i >= 0; i--)
		{
			if (phi[i] != -1 && (i+1)%(i-phi[i]) == 0)
			{
				answer = i+1;
				break;
			}
		}
		cout << answer<< "\n";
	}
	return 0;
}