Cod sursa(job #1315282)

Utilizator avaspAva Spataru avasp Data 12 ianuarie 2015 18:19:21
Problema Prefix Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1 kb
#include<cstdio>
#include<cstring>
using namespace std;
char s[1000001];
int n,max,pi[1000001],k,c1,c2;

void preprocesare(){
    pi[0]=0;
    k=0;
    for(int q=1;q<=n+1;q++){
        while((k>0)&&s[k]!=s[q])
            k=pi[k-1];
        if(s[k]==s[q])
            k++;
        pi[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;
        preprocesare();
        max=0;
        for(int j=1;j<=n+1;j++){
            if(pi[j]==(j+1)/2&&(j+1)%2==0)
                max=(j+1)/2;
            pi[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);
        }
    }
    return 0;
}