Cod sursa(job #19495)

Utilizator cimiCristina Stancu-Mara cimi Data 19 februarie 2007 18:04:20
Problema Prefix Scor 0
Compilator fpc Status done
Runda Arhiva de probleme Marime 0.77 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;
    if pi[q]>pi[sol] then sol:=q;
  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;
    sol:=1;
    prefix;
    m:=sol-pi[sol];
    m:=(sol div m)*m;
    if m>1
      then writeln(m)
      else writeln('0');
    dec(test);
  end;
  close(output);
  close(input);

end.