Cod sursa(job #3278357)

Utilizator Mihai_999Diaconeasa Mihai Mihai_999 Data 19 februarie 2025 15:41:36
Problema Prefix Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.66 kb
#include <iostream>
#include <fstream>
#define nl '\n'

using namespace std;

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

const int NMAX = 1e6+5;

int n, pi[NMAX], ans;
string s;

void solve()
{
    ans = 0;
    fin >> s;
    n = s.size();
    int j = 0;
    for (int i = 1; i < n; i++)
    {
        while (j > 0 && s[i] != s[j])
            j = pi[j-1];
        if (s[i] == s[j])
            j++;
        pi[i] = j;
        if (j > 0 && (i+1) % (i+1-j) == 0)
            ans = i+1;
    }
    fout << ans << nl;
    return;
}

int main()
{
    int t;
    fin >> t;
    while (t--)
        solve();
    return 0;
}