Cod sursa(job #2629349)

Utilizator andreiomd1Onut Andrei andreiomd1 Data 20 iunie 2020 12:19:51
Problema Prefix Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.18 kb
#include <fstream>
#include <cstring>

using namespace std;

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

const int NMAX = 1e6 + 5;

int T, N;

char S[NMAX];

int phi[NMAX];

static inline void Read ()
{
    f >> (S + 1);

    N = (int)strlen(S + 1);

    return;
}

static inline void KMP ()
{
    phi[1] = 0;

    int ans = 0;

    for(int i = 2; i <= N; ++i)
    {
        while(ans && S[i] != S[ans + 1])
            ans = phi[ans];

        if(S[i] == S[ans + 1])
            ++ans;

        phi[i] = ans;
    }

    phi[1] = 1;

    return;
}

static inline void Find_Sol ()
{
    for(int Lg = N; Lg > 1; --Lg)
        if(phi[Lg] && Lg % (Lg - phi[Lg]) == 0)
        {
            g << Lg << '\n';

            return;
        }

    g << 0 << '\n';

    return;
}

static inline void Reset ()
{
    for(int i = 1; i <= N; ++i)
        S[i] = phi[i] = 0;

    N = 0;

    return;
}

static inline void TestCase ()
{
    Read();

    KMP();

    Find_Sol();

    if(T)
        Reset();

    return;
}

int main()
{
    f.tie(nullptr);

    f >> T;

    while(T--)
        TestCase();

    return 0;
}