Cod sursa(job #614055)

Utilizator stay_awake77Cangea Catalina stay_awake77 Data 5 octombrie 2011 15:39:44
Problema Prefix Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.94 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--; )
    {
        memset( Phi, 0, sizeof(Phi) );
        memset( LgPer, 0, sizeof(LgPer) );

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

        end = LgMax = 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( 2*end >= i && LgPer[end] == i - end )
            {
                LgMax = i;
                LgPer[i] = i - end;
            }
            else if( 2*end == i )
            {
                LgMax = i;
                LgPer[i] = end;
            }
        }

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

    return 0;
}