Cod sursa(job #1802404)

Utilizator MiricaMateiMirica Matei MiricaMatei Data 10 noiembrie 2016 11:45:23
Problema Prefix Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.74 kb
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
char s[1000005];
int p[1000005];
void prefix(int n){
  int x = 0, i;
  for (i = 2; i <= n; i ++){
    while (x > 0 && s[x + 1] != s[i])
      x = p[x];
    if (s[x + 1] == s[i])
      x ++;
    p[i] = x;
  }
}
int main(){
  freopen("prefix.in", "r", stdin);
  freopen("prefix.out", "w", stdout);
  int i, j, t, n, ok;
  scanf("%d ", &t);
  for (i = 1; i <= t;i ++){
    gets(s + 1);
    n = strlen(s + 1);
    ok = 0;
    prefix(n);
    for (j = n; j >= 1; j --)
      if (p[j] != j && p[j] != 0 && j %(j - p[j]) == 0){
        printf("%d\n", j);
        ok = 1;
        break;
      }
    if (ok == 0)
      printf("0\n");
  }
  return 0;
}