Cod sursa(job #1417658)

Utilizator gallexdAlex Gabor gallexd Data 10 aprilie 2015 19:13:59
Problema Prefix Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.61 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 (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;
}