Cod sursa(job #2460461)

Utilizator gavra_bogdanBogdan Gavra gavra_bogdan Data 23 septembrie 2019 19:15:43
Problema Prefix Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.88 kb
#include <bits/stdc++.h>

using namespace std;
const int nmax=1000005;
int phi[nmax];
int main()
{
    ifstream cin("prefix.in");
    ofstream cout("prefix.out");
    string s;
    int t;
    cin>>t;
    while(t--)
    {
        cin>>s;
        int n=s.size();
        memset(phi,0,sizeof(phi));
        phi[0]=-1;
        for(int i=1; i<n; i++)
        {
            long long aux=i-1;
            while(s[phi[aux]+1]!=s[i]&&phi[aux]!=-1)
                aux=phi[aux];
            if (s[phi[aux]+1]==s[i])
                phi[i]=phi[aux]+1;
            else
                phi[i]=-1;
        }
        int ans=0;
        for(int i=s.size()-1; i>=0; i--)
            if(phi[i]!=-1)
                if((i+1)%(i-phi[i])==0)
                {
                    ans=i+1;
                    break;
                }
        cout<<ans<<"\n";
    }
    return 0;
}