Pagini recente » Cod sursa (job #3247662) | Cod sursa (job #3196238) | Cod sursa (job #5001) | Cod sursa (job #3282399) | Cod sursa (job #1936724)
#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;
}