Cod sursa(job #1284871)

Utilizator witselWitsel Andrei witsel Data 6 decembrie 2014 21:28:16
Problema Prefix Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.69 kb
#include <iostream>
#include <fstream>
#include <string.h>
#define NMAX 1000000
using namespace std;
ifstream fin("prefix.in");
ofstream fout("prefix.out");
int p[NMAX],n,i,T;
char a[NMAX];
int main()
{
    fin>>T;
    while(T--)
    {
        fin>>(a+1);

        for(unsigned i=2;a[i]!=0;++i)
        {
            int q=p[i-1];
            while(q && a[q+1]!=a[i])
                q=p[q];
            if(a[q+1]==a[i])
                p[i]=q+1;

        }
        int sol=0;
        for(int i=strlen(a+1);i>=1;--i)
            if(p[i]!=i&&p[i]!=0 &&i%(i-p[i])==0)
        {
            sol=i;
            break;
        }
        fout<<sol<<"\n";
    }
    return 0;
}