Cod sursa(job #1212968)

Utilizator RusuAlexeiRusu Alexei RusuAlexei Data 26 iulie 2014 18:42:55
Problema Prefix Scor 70
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.19 kb
program  prefix;
  var buf:array[1..100000] of longint;
      t,j,k,n,i,p,max:longint;
      pre:array [1..1000001] of longint;
      s:ansistring;
      b:boolean;


function min(u,v:longint):longint;
  begin
    if u>v then min:=v else min:=u;
  end;

begin
  assign(input,'prefix.in');
  reset(input);
  settextbuf(input,buf);
  assign(output,'prefix.out');
  rewrite(output);

  readln(t);
  for k:=1 to t do
    begin
      max:=0;
      readln(s);
      s:=s+'#';
      n:=length(s);
      pre[1]:=0;
      for i:=2 to n do
        begin
          j:=pre[i-1];
          while (j>0)and(s[i]<>s[j+1]) do j:=pre[j];
          if s[i]=s[j+1] then pre[i]:=j+1 else pre[i]:=0;
        end;
      i:=1;
      b:=false;p:=1;
      while i<n do
        begin
          inc(i);
          if (pre[i]<>pre[i-1]+1) then b:=false;
          if (not b)and(pre[i-1]>0) then
            begin
              if i-1>=2*p then
              if i-1 -(i-1)mod p >max then max:=i-1-(i-1)mod p;
            end;
          if pre[i]=1 then
            begin
              p:=i-1;
              b:=true;
            end;
        end;
      writeln(max);
    end;
  close(output);
end.