Cod sursa(job #614044)

Utilizator stay_awake77Cangea Catalina stay_awake77 Data 5 octombrie 2011 15:11:55
Problema Prefix Scor 70
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.78 kb
#include <fstream>
#include <cstring>

#define LMAX 1000005

using namespace std;

ifstream in("prefix.in");
ofstream out("prefix.out");

int LS, i, T, end, Phi[LMAX], LgPer[LMAX], LgMax = 0;
char S[LMAX];

int main()
{
    in >> T;
    for( ; T--; )
    {
        LgMax = 0;
        memset( Phi, 0, sizeof(Phi) );
        memset( LgPer, 0, sizeof(LgPer) );
        memset( S, 0, sizeof(S) );

        in >> (S+1);
        LS = strlen(S+1);

        end = 0;
        for( i = 2; i < LS; ++i )
        {
            for( ; end && S[end+1] != S[i]; end = Phi[end] );
            if( S[end+1] == S[i] ) ++end;
            Phi[i] = end;

            if( Phi[i] && !(i%(i-Phi[i])) ) LgMax = i;
        }

        out << LgMax << '\n';
    }

    return 0;
}