Cod sursa(job #1936724)

Utilizator AkrielAkriel Akriel Data 23 martie 2017 12:49:57
Problema Prefix Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.31 kb
#include <bits/stdc++.h>

using namespace std;

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

const int N = 1000005;

string text;

int lengthText, questions, maximumLength;

int maximum, positionMaximum, mainPeriodLength, solution;;

int prefix[N];

void calculatingPrefixes()
{
    int matches = 0;
    for ( int index = 2; index <= lengthText; index++ )
    {
        for ( ; text[index] != text[matches+1] and matches; )
            matches = prefix[matches];
        if ( text[index] == text[matches+1] )
            matches++;
        prefix[index] = matches;
    }
}

int main()
{
    fin >> questions;
    for ( ; questions; questions-- )
    {
        fin >> text;
        lengthText = text.size();
        maximumLength = max(maximumLength, lengthText);
        text = " " + text;
        int matches = 0;
        for ( int index = 2; index <= lengthText; index++ )
        {
            for ( ; text[index] != text[matches+1] and matches; )
                matches = prefix[matches];
            if ( text[index] == text[matches+1] )
                matches++;
            if(matches && ( index % (index - matches) == 0 ))
                 solution = index;
            prefix[index] = matches;
        }
        fout << solution << "\n";
    }
    return 0;
}