Pagini recente » Cod sursa (job #1440309) | Cod sursa (job #3158902) | Cod sursa (job #3174878) | Cod sursa (job #2817998) | Cod sursa (job #2759475)
#include <fstream>
#include <string.h>
#define NMAX 1000005
using namespace std;
ifstream f("prefix.in");
ofstream g("prefix.out");
int questions;
char str[NMAX];
int pi[NMAX];
int main()
{
f >> questions;
while (questions--) {
f >> (str + 1);
pi[1] = 0;
int pre = 0;
int answer = 0;
int length = strlen(str + 1);
for (int i = 2; i <= length; i++) {
while (pre != 0 && str[i] != str[pre + 1]) {
pre = pi[pre];
}
if (str[i] == str[pre + 1])
pre++;
pi[i] = pre;
if (pi[i] != 0 && i % (i - pi[i]) == 0)
answer = i;
}
g << answer << "\n";
}
return 0;
}