Pagini recente » Cod sursa (job #302960) | Cod sursa (job #1261645) | Cod sursa (job #3182140) | Cod sursa (job #613050) | Cod sursa (job #1575295)
#include <fstream>
#include <cstring>
using namespace std;
ifstream fin("prefix.in");
ofstream fout("prefix.out");
char s[1000010];
int N, P[1000010];
int main()
{
fin >> N;
while(N --)
{
fin >> (s + 1);
int lenghtS = strlen(s + 1);
int x = 0;
for(int i = 2; i <= lenghtS; i ++)
{
while( x && s[i] != s[x + 1])
{
x = P[x];
}
if(s[i] == s[x + 1])
{
x ++;
}
P[i] = x;
}
bool ok = false;
for(int i = lenghtS; i > 0; i --)
{
if(P[i] && (i % (i - P[i]) == 0))
{
fout << i << '\n';
ok = true;
break;
}
}
if(!ok)
{
fout << 0 << '\n';
}
memset(P, 0, sizeof(P));
}
return 0;
}