Cod sursa(job #2089860)

Utilizator FrequeAlex Iordachescu Freque Data 17 decembrie 2017 12:06:52
Problema Prefix Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.84 kb
#include <iostream>
#include <fstream>
#include <cstring>

using namespace std;

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

const int CMAX = 1000000 + 5;

int teste, len;
int phi[CMAX];
char text[CMAX];

void prefix()
{
    memset(phi, 0, sizeof(phi));
    for (int i = 2; i <= len; ++i)
    {
        int last = phi[i - 1];
        while (last > 0 && text[i] != text[last + 1]) last = phi[last];
        if (text[i] == text[last + 1]) phi[i] = last + 1;
    }
}

int main()
{
    fin >> teste;
    while (teste--)
    {
        int maxx = 0;
        fin >> (text + 1);
        len = strlen(text + 1);

        prefix();

        for (int i = 1; i <= len; ++i)
            if (i % (i - phi[i]) == 0 && phi[i])
                maxx = max(maxx, i);

        fout << maxx << '\n';
    }

    return 0;
}