Cod sursa(job #2440518)

Utilizator claudiu.gatinaFMI Claudiu Gatina claudiu.gatina Data 18 iulie 2019 16:56:48
Problema Prefix Scor 40
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.99 kb
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include <algorithm>
#include <set>
#include <map>

#define NMAX 1000002

using namespace std;

const long long PRIM1 = 179424673;
char s[NMAX];

void solve()
{
	map<long long int, long long int> m1;

	long long int h1 = 0;

	long long int r1 = 27;

	scanf("%s", &s);

	int maxLen = 0;

	for (int i = 0; s[i] != NULL; ++i)
	{
		h1 += ((long long int)(s[i] - 'a' + 1)) * r1;
		h1 %= PRIM1;

		bool ok = (s[i + 1] == s[0]);

		if (m1[h1])
		{
			maxLen = i + 1;

			if (ok)
			{
				long long int root1 = m1[h1];
				m1[(h1 + r1 * root1) % PRIM1] = root1;
			}

		}

		if (ok)
		{
			m1[(h1 + r1 * h1) % PRIM1] = h1;
		}

		r1 = (r1 * 27) % PRIM1;
	}
	

	printf("%d\n", maxLen);
}

int main()
{
	freopen("prefix.in", "r", stdin);
	freopen("prefix.out", "w", stdout);

	int t;
	scanf("%d", &t);
	
	for (int i = 0; i < t; ++i)
		solve();


	

	return 0;
}