Pagini recente » Profil saibot94 | Cod sursa (job #13963) | Cod sursa (job #253684) | Monitorul de evaluare | Cod sursa (job #1936355)
#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";
}
}