Cod sursa(job #3321104)

Utilizator Luca_georgescuLuca Georgescu Luca_georgescu Data 8 noiembrie 2025 11:00:22
Problema Prefix Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.99 kb
#include <bits/stdc++.h>

using namespace std;

ifstream f("prefix.in");
ofstream g("prefix.out");

const int nmax=2e6+5;
string s;

vector <int> create_lps(int n)
{
    int i=1, lg=0;

    vector <int> lps(n,0);

    while ( i<n )
    {
        if ( s[i]==s[lg] )
        {
            lg++;
            lps[i]=lg;
            i++;
        }
        else if ( lg ) lg=lps[lg-1];
        else
        {
            lps[i]=0;
            i++;
        }
    }

    return lps;
}

int periodic(int n)
{
    vector <int> lps=create_lps(n);

    int maxi=0;

    // bucatile s[0..k-1] si s[lg-k..lg-1] sunt identice
    for (int i=0; i<n; i++ )
    {
        int lg=i+1;

        int k=lps[i];
        if ( k==0 ) continue;

        int p=lg-k;

        if ( lg%p==0 && lg>maxi ) maxi=lg;
    }

    return maxi;
}

int main()
{
    int t; f >> t;

    while ( t-- )
    {
        f >> s;
        g << periodic(s.size()) << '\n';
    }
    return 0;
}