Cod sursa(job #1936355)

Utilizator AkrielAkriel Akriel Data 23 martie 2017 00:43:22
Problema Prefix Scor 70
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.48 kb
#include <bits/stdc++.h>

using namespace std;

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

const int N = 1000005;

string text;

int lengthText, matches, questions, maximumLength;

int maximum, positionMaximum, mainPeriodLength;

int prefix[N];

int solution;

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

int main(){
    fin >> questions;
    for ( ; questions; questions-- ){
        fin >> text;
        lengthText = text.size();
        maximumLength = max(maximumLength, lengthText);
        text = " " + text;
        memset(prefix, maximumLength, 0);
        maximum = positionMaximum = solution = 0;

        calculatingPrefixes();

        for ( int index = 1; index <= lengthText; index++ ){
            if ( prefix[index] > maximum ){
                maximum = prefix[index];
                positionMaximum = index;
            }
        }
        mainPeriodLength = positionMaximum - maximum;

        for ( int index = positionMaximum; prefix[index]; index-- ){
            if ( index % mainPeriodLength == 0 ){
                solution = max(solution, index);
                break;
            }
        }
        fout << solution << "\n";
    }
}