Cod sursa(job #2990796)

Utilizator Theo20067Cismaru Theodor-Alexe Theo20067 Data 8 martie 2023 16:07:13
Problema Prefix Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.81 kb
#include <fstream>
#include <cstring>
using namespace std;
ifstream fin ("prefix.in");
ofstream fout("prefix.out");
char S[1000002];
int P[1000002];
int a,b,i,sol,L,nr,n,j;
int main ()
{
    fin>>n;
    for(j=1;j<=n;j++)
    {
        fin.get();
        fin.get(S,1000001);
        a=strlen(S);
        for(i=a;i>=1;i--)
            S[i]=S[i-1];
        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
**/