Cod sursa(job #3278359)

Utilizator Mihai_999Diaconeasa Mihai Mihai_999 Data 19 februarie 2025 15:44:48
Problema Prefix Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.61 kb
#include <iostream>
#include <fstream>
using namespace std;

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

const int NMAX=1e6+5;

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

int main()
{
    int t;
    fin>>t;
    while(t--)
    {
        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&&(i+1)%(i+1-j)==0)
                ans=i+1;
        }
        fout<<ans<<'\n';
    }
    return 0;
}