Cod sursa(job #303872)
#include<stdio.h>
#include<string.h>
#define NM 1000001
char t[NM];
int u[NM],n;
void urm(char*p){
int q,k,l;
l=strlen(p);
k=-1;
u[0]=-1;
for(q=1;q<l;++q){
while(k>-1&&p[k+1]!=p[q]) k=u[k];
if(p[k+1]==p[q]) k++;
u[q]=k;
}
}
int main(){
freopen("prefix.in","r",stdin);
freopen("prefix.out","w",stdout);
int test,i,n,poz,lmax,lc,j,pozf,ll;
scanf("%d\n",&test);
while(test--){
scanf("%s",t);
n=strlen(t);
urm(t);
lc=-1;lmax=0;
i=0;poz=0;
while(i<n){
while(i<n&&u[i]!=0) i++;
if(i==n)break;
j=i+1;
if(j==n) break;
while(j<n&&u[j]==u[j-1]+1) j++;
lc=j-i;
if(lc>lmax) lmax=lc,poz=i,pozf=j-1;
i=j;
}
if(poz)
ll=lmax/poz*poz+poz;
else ll=0;
printf("%d\n",ll);
}
return 0;
}