Cod sursa(job #568466)

Utilizator andrei31Andrei Datcu andrei31 Data 31 martie 2011 11:25:56
Problema Prefix Scor 80
Compilator fpc Status done
Runda Arhiva de probleme Marime 0.51 kb
var t:array[1..1000000] of char;
    urm:array[1..1000000] of longword;
    n,j,s,k,i:longword;
begin
assign(input,'prefix.in');reset(input);
assign(output,'prefix.out');rewrite(output);
readln(n);
for j:=1 to n do
 begin
 readln(t);
 i:=2;k:=0;urm[1]:=0;s:=0;
  while t[i]<>#0 do
   begin
   while (k>0) and (t[k+1]<>t[i]) do k:=urm[k];
   if t[k+1]=t[i] then inc(k);
   urm[i]:=k;
   if (k>0) and (i mod (i-k)=0) then s:=i;
   inc(i);
   end;
  writeln(s);
 end;
 close(output);close(input);
end.