Cod sursa(job #1469532)

Utilizator dorumusuroiFMI - Doru Musuroi dorumusuroi Data 8 august 2015 16:30:32
Problema Prefix Scor 70
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.81 kb
#include <cstdio>
#include <cstring>
using namespace std;
const char iname[] = "prefix.in";
const char oname[] = "prefix.out";
const int lmax = 1000010;
int pr[lmax];
char P[lmax];
void getPrefix()
{
    int poz = 0, dim = 0;
    pr[0] = -1;
    int m = strlen(P);
    int k = -1;
    for(int q = 1; q < m; q++)
    {
        while(k>-1 && P[k+1] != P[q])
            k = pr[k];
        if(P[k+1] == P[q])
            k++;
        if(k + 1 == 1)  poz = q;
        if((poz > 0) && (k+1 > 0) &&((k+1)%poz == 0)) dim = q+1;
        pr[q] = k;
    }
    printf("%d\n", dim);
}

int main()
{
    freopen(iname,"r",stdin);
    freopen(oname, "w", stdout);
    int T;
    scanf("%d\n", &T);
    for(int q = 0; q < T; q++)
    {
        scanf("%s\n",P);
        getPrefix();
    }
    return 0;
}