Cod sursa(job #1417695)

Utilizator gallexdAlex Gabor gallexd Data 10 aprilie 2015 20:09:48
Problema Prefix Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.74 kb
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>

using namespace std;

int N, len;
//char str[1000100];
string str;
int table[1000100];

void prefix() {
  int match = 0, maxim = 0;
  table[0] = 0;
  for (int i=1; i<len; ++i) {
    while (match > 0 && str[match] != str[i])
      match = table[match-1];
    if (str[match] == str[i]) {
      match++;
      if ((i+1) % (i-match+1) == 0) 
	maxim = i+1;
    }
    table[i] = match;
  }
  printf("%d\n", maxim);
}

int main () {

  freopen("prefix.in", "rt", stdin);
  freopen("prefix.out", "wt", stdout);
  
  //scanf("%d", &N);
  cin >> N;
  for (;N;--N) {
    //scanf("%s", str+1);
    //len = strlen(str+1);
    cin >> str;
    len = str.size();
    prefix();
  }
  return 0;
}