Cod sursa(job #1223051)

Utilizator mihaimusatMihai Musat mihaimusat Data 25 august 2014 12:33:34
Problema Prefix Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.8 kb
#include<cstdio>
#include<cstring>

using namespace std;

char s[1000005];
int n,per[1000005];

int main()
{
    freopen("prefix.in","r",stdin);
    freopen("prefix.out","w",stdout);
    int n,i,t;
    scanf("%d",&n);
    gets(s+1);
    for(t=1;t<=n;t++)
    {
        gets(s+1);
        int x;
        per[0]=0;
        x=0;
        for(i=2;s[i]!=0;i++)
        {
            while(x && s[i]!=s[x+1])
                x=per[x];
            if(s[i]==s[x+1])
                x++;
            per[i]=x;
        }
        int ok=0;
        for(i=strlen(s+1);i;i--)
            if(per[i]!=i && i %(i-per[i])==0 && per[i])
            {
                printf("%d\n",i);
                ok=1;
                break;
            }
        if(ok==0) printf("0\n");
    }
    return 0;
}