Cod sursa(job #3161734)

Utilizator vladsoartavlad sofronea vladsoarta Data 27 octombrie 2023 20:07:41
Problema Prefix Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.08 kb
#include <fstream>

using namespace std;
ifstream cin("prefix.in");
ofstream cout("prefix.out");


int main()
{int n;
   cin>>n;
   for(int i=1;i<=n;i++)
   {
       int lps[1000001]={0};
       string s;
       cin>>s;

       int k=0;
       for(int j=1;j<(int)s.size();j++)
       {
           while(k!=0&&s[j]!=s[k])
            k=lps[k-1];
           if(s[j]==s[k])
            k++;
            lps[j]=k;
       }

       int gasit=0;
       int j=s.size()-1;

       while(j>0&&!gasit)
       {
           if(lps[j])
           {
               int pos=j;
               int lungcheck = j-lps[j]+1;
               int repet = 0;
               while(pos-lps[pos]+1==lungcheck&&lps[pos])
               {
                   pos-=lungcheck;
                   repet++;
               }
               if(pos+1==lungcheck)
               {repet++;
                   cout<<repet*lungcheck<<'\n';
                   gasit=1;
               }

           }
            j--;
       }
       if(!gasit)
        cout<<0<<'\n';

   }
    return 0;
}