Cod sursa(job #1325676)

Utilizator OlaruSabinOlaru Sabin OlaruSabin Data 24 ianuarie 2015 11:35:45
Problema Prefix Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.02 kb
#include<cstdio>
#include<cstring>
int p[1000001],k,n,max,c1,c2;
char s[1000001];
using namespace std;
void prefix()
{
    p[0]=0;
    k=0;
    for(int q=1;q<=n+1;q++)
    {
        while((k>0)&&s[k]!=s[q])
            k=p[k-1];
        if(s[k]==s[q])
            ++k;
        p[q]=k;
    }
}
int main()
{
    int t;
    freopen("prefix.in","r",stdin);
    freopen("prefix.out","w",stdout);
    scanf("%d\n",&t);
    for(int i=1;i<=t;i++)
    {
       scanf("%s\n",&s);
        n=strlen(s)-1;
        prefix();

        max=0;
        for(int j=1;j<=n+1;j++)
        {
            if(p[i]==(j+1)/2 && (j+1)%2==0)
            {
                max=(j+1)/2;
            }
            p[j]=0;
        }
        if(max==0)
            printf("0\n");
        else{
        c1=0;
        c2=max;
        while(s[c1]==s[c2]&&c2<=n){
            c1++;
            c2++;
        }
        if(c2%max==0)
            printf("%d\n",c2);
        else
            printf("%d\n",c2-c2%max);
        }
    }
}