Cod sursa(job #1493172)

Utilizator pepsiM4A1Ozturk Arif pepsiM4A1 Data 28 septembrie 2015 20:22:17
Problema Prefix Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.77 kb
#include <cstdio>
#include <cstring>
int t;
int p[1000023];
char ch[1000023];
int main()
{
    freopen ("prefix2.in","r",stdin);
    freopen ("prefix2.out","w",stdout);
    scanf("%d",&t);
    for(int xyz=1;xyz<=t;xyz++)
    {
        scanf("%s",(ch+1));
        int lun=strlen(ch+1);
        for(int i=1;i<=lun;i++) p[i]=0;
        int k=0;
        for(int i=2;i<=lun;i++)
        {
            while(k>0&&ch[k+1]!=ch[i]) k=p[k];
            if(ch[k+1]==ch[i]) k++;
            p[i]=k;
        }
        bool as=0;
        for(int i=lun;i>=2;i--)
        {
            if(p[i]!=0&&i%(i-p[i])==0)
            {
                as=1;
                printf("%d\n",i);
                break;
            }
        }
        if(as==0) printf("0\n");
    }
}