Cod sursa(job #1633478)

Utilizator roxannemafteiuMafteiu-Scai Roxana roxannemafteiu Data 6 martie 2016 12:15:58
Problema Prefix Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.65 kb
#include <fstream>

using namespace std;

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

const int LEN_MAX = 1e6;

int T;
char v[LEN_MAX + 5];
int p[LEN_MAX + 5];

int compute_prefix()
{
    fin.getline(v + 1, LEN_MAX + 2);

    int i = 2, q;
    for(q = 0; v[i]; ++i)
    {
        while(q > 0 && v[q + 1] != v[i])
            q = p[q]; //next character does not match
        if(v[q + 1] == v[i])
            ++q; // next character matches
        p[i] = q;
    }

    while(i > 0  && (p[i] == 0 || p[i] % (i - p[i]) != 0))
        --i;

    return i;
}
int main()
{
    fin >> T;
    while(T--)
        fout << compute_prefix() << "\n";
    return 0;
}