Cod sursa(job #2990798)

Utilizator Theo20067Cismaru Theodor-Alexe Theo20067 Data 8 martie 2023 16:12:14
Problema Prefix Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.73 kb
#include <fstream>
#include <cstring>
using namespace std;
ifstream fin ("prefix.in");
ofstream fout("prefix.out");
string S;
int P[1000002];
int a,b,i,sol,L,nr,n,j;
int main ()
{
    fin>>n;
    for(j=1;j<=n;j++)
    {
        fin>>S;
        S='$'+S;
        a=S.length();
        L=0;
        for(i=2;i<a;i++)
        {
            while(L!=0&&S[i]!=S[L+1])
                L=P[L];
            if(S[i]==S[L+1])
                L++;
            P[i]=L;
            nr=i-P[i];
            if(P[i]!=0&&i%nr==0)
                sol=i;
            //fout<<P[i]<<" ";
        }
        for(i=1;i<=a;i++)
            P[i]=0;

        //fout<<"\n";
        fout<<sol<<"\n";
    }
    return 0;
}
/**
1
xxxxaxax
**/