Cod sursa(job #1417709)

Utilizator gallexdAlex Gabor gallexd Data 10 aprilie 2015 20:36:36
Problema Prefix Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.68 kb
#include <cstdio>
#include <cstring>
 
int N, len;
char str[1000100];
int table[1000100];
 
void prefix() {
  int match = 0, maxim = 0;
  table[1] = 0;
  for (int i=2; i<=len; ++i) {
    while (match > 0 && str[match+1] != str[i]) {
      match = table[match];
      if ((i - match) * 2 > len) {
	i = len + 1;
	continue;
      }
    }
    if (str[match+1] == str[i]) {
      match++;
      if (i % (i-match) == 0) 
	maxim = i;
    }
    table[i] = match;
  }
  printf("%d\n", maxim);
}
 
int main () {
 
  freopen("prefix.in", "rt", stdin);
  freopen("prefix.out", "wt", stdout);
   
  scanf("%d", &N);
  for (;N;--N) {
    scanf("%s", str+1);
    len = strlen(str+1);
    prefix();
  }
  return 0;
}