Cod sursa(job #613533)

Utilizator stay_awake77Cangea Catalina stay_awake77 Data 29 septembrie 2011 09:58:34
Problema Prefix Scor 70
Compilator cpp Status done
Runda Arhiva de probleme Marime 1 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) );

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

        if( LgMax < LS ) out << LgMax << '\n';
        else out << "0\n";
    }

    return 0;
}