Cod sursa(job #3329037)

Utilizator MilitaruMihaiMihaiMIlitaru MilitaruMihai Data 11 decembrie 2025 16:33:03
Problema Prefix Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.65 kb
#include <bits/stdc++.h>

using namespace std;
ifstream fin("prefix.in");
ofstream fout("prefix.out");
const int lim = 1e6 + 5;
int t, n, lps[lim], ans;
char a[lim];
void precalc()
{
    int k = 0;
    for (int i = 2; i <= n; ++i)
    {
        while (k && a[k] != a[i - 1]) k = lps[k];
        if (a[k] == a[i - 1]) k++;
        lps[i] = k;
        if (lps[i] > 0 && i % (i - lps[i]) == 0) ans = i;
    }
    for (int i = 0; i <= n ; ++i) lps[i] = 0;
}

void testcase()
{
    fin >> a;
    n = strlen(a);
    precalc();
    fout << ans <<'\n';
}

int main()
{
    fin >> t;
    for (; t; --t)
        testcase();
    return 0;
}