Cod sursa(job #19519)

Utilizator cimiCristina Stancu-Mara cimi Data 19 februarie 2007 18:23:42
Problema Prefix Scor 80
Compilator fpc Status done
Runda Arhiva de probleme Marime 0.79 kb
const
  lim=1000100;
var
  pi:array[1..lim] of longint;
  t:array[1..lim] of char;
  m,k,test,q,i,j,n,sol:longint;

procedure prefix;
begin
  k:=0;
  for q:=2 to n do
  begin
    while (k>0) and (t[k+1]<>t[q]) do k:=pi[k];
    if t[k+1]=t[q] then inc(k);
    pi[q]:=k;
  end;
end;

begin
  assign(input,'prefix.in');
  reset(input);
  assign(output,'prefix.out');
  rewrite(output);
  readln(test);
  while test>0 do
  begin
    n:=0;
    while not seekeoln do
    begin
      inc(n);
      read(t[n]);
    end;
    readln;
    prefix;
    sol:=0;
    for m:=1 to n do
      if (pi[m]>0) and (m mod (m-pi[m])=0) and (m>sol) then sol:=m;
    if sol>1
      then writeln(sol)
      else writeln('0');
    dec(test);
  end;
  close(output);
  close(input);

end.