Cod sursa(job #177906)

Utilizator tamas_iuliaTamas Iulia tamas_iulia Data 13 aprilie 2008 20:26:21
Problema Prefix Scor 40
Compilator fpc Status done
Runda Arhiva de probleme Marime 0.69 kb
var a : array[1..1000000] of char;
    pi : array[1..1000000] of integer;
    q,t,i,j,k,max,n : longint;
    f,g : text;
procedure kmp;
begin
  k:=0;pi[1]:=0;
    for i:=2 to n do
    begin
      while (k>0) and(a[k+1]<>a[i]) do
        k:=pi[k];
      if a[k+1]=a[i] then inc(k);
      pi[i]:=k;
    end;
end;

begin
assign(f,'prefix.in');reset(f);
assign(g,'prefix.out');rewrite(g);
readln(f,t);
for q:=1 to t do
begin
  n:=0; max:=0;
  while not eoln(f) do
  begin
    inc(n);
    read(f,a[n]);
  end;
  readln(f);
  kmp;
  for k:=1 to n do
    if k-pi[k]>0 then
       if (k mod(k-pi[k])=0)and(k>max)and(pi[k]>0)then max:=k;

  writeln(g,max);
end;
close(g);
end.