Cod sursa(job #564222)

Utilizator TeodoraTanaseTeodora Tanase TeodoraTanase Data 26 martie 2011 22:14:02
Problema Prefix Scor 100
Compilator cpp Status done
Runda gr_3 Marime 0.65 kb
#include <cstdio>
#include <cstring>
#include <algorithm>
#define M 1000005

using namespace std;

int t, b[M], n, poz;
char a[M];

void kmp()
{
    int i=0, j=-1;
    b[i]=j;
    while (i<n)
    {
        while (j>=0 && a[i]!=a[j])
            j=b[j];
        i++;
        j++;
        b[i]=j;
        if (j>0 && i%(i-j)==0)
            poz=i;
    }
    printf ("%d\n",poz);
}

int main()
{
    freopen ("prefix.in","r",stdin);
    freopen ("prefix.out","w",stdout);
    scanf ("%d ",&t);
    for (int z=1; z<=t; z++)
    {
        fgets(a,M,stdin);
        n=strlen(a)-1;
        poz=0;
        kmp();
    }
    return 0;
}