Cod sursa(job #335327)
| Utilizator | Data | 29 iulie 2009 15:43:41 | |
|---|---|---|---|
| Problema | Prefix | Scor | 100 |
| Compilator | cpp | Status | done |
| Runda | Arhiva de probleme | Marime | 0.72 kb |
#include <stdio.h>
#include <string.h>
#define Nmax 1000100
char a[Nmax];
int t,pi[Nmax],l,k,nr;
int main()
{
freopen("prefix.in","r",stdin);
freopen("prefix.out","w",stdout);
int q;
scanf("%d\n",&t);
while(t--)
{
gets(a+1);
l=strlen(a+1);
k=0;
nr=0;
pi[1]=0;
for(q=2;q<=l;++q)
{
while(k>0 && a[k+1]!=a[q]) k=pi[k];
if(a[k+1]==a[q])
++k;
pi[q]=k;
if(k && (q%(q-k)==0)) nr=q;
}
printf("%d\n",nr);
}
return 0;
}
