Cod sursa(job #2298449)

Utilizator AlexandruabcdeDobleaga Alexandru Alexandruabcde Data 8 decembrie 2018 10:32:38
Problema Prefix Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.91 kb
#include <bits/stdc++.h>

using namespace std;

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

const int NMax = 1000005;

int t;

int n;

int phi[NMax];

char ch[NMax+5];

void construire_phi (int n)
{
    phi[0] = 0;
    int rez = 0;

    for (int i = 1; i < n; i++)
    {
        while (rez > 0 && ch[i] != ch[rez])
        {
            rez = phi[rez-1];
        }

        if (ch[rez] == ch[i]) rez++;

        phi[i] = rez;
    }
}

void solve()
{
    int Max = 0;

    for (int i = 0; i < n; i++)
    {
        if ( (i + 1) - phi[i] < i + 1 && (i + 1) % ((i + 1) - phi[i]) == 0)
        {
            Max = i + 1;
        }
    }

    g << Max << '\n';
}

int test()
{
    f.getline(ch, NMax);
    n = strlen(ch);

    construire_phi(n);
    solve();
}

int main()
{
    f >> t;

    f.get();

    for (; t; t--)
        test();
    return 0;
}