MSTRING - String problem

Tác giả: RR

Ngôn ngữ: Pascal

//Written by RR
{$MODE OBJFPC}

uses math;
const
  FINP          =       '';
  FOUT          =       '';
  MAXN          =       1011;

var
  f1,f2         :       text;
  f             :       array[0..MAXN,0..MAXN] of longint;
  next          :       array[1..MAXN,char] of longint;
  l1,l2         :       longint;
  s1,s2         :       ansistring;

procedure openF;
    begin
      assign(f1,FINP); reset(f1);
      assign(f2,FOUT); rewrite(f2);
    end;

procedure closeF;
    begin
      close(f1);
      close(f2);
    end;

procedure inp;
    begin
      readln(f1,s1); l1:=length(s1);
      readln(f1,s2); l2:=length(s2);
    end;

procedure solve;
    var
      i,j,u:longint;
    begin
      for i:=1 to l1 do
        begin
          f[i,1]:=max(f[i-1,1],next[1,s1[i]]);
          for j:=2 to i do
            begin
              u:=f[i-1,j-1]+1;
              f[i,j]:=max(f[i-1,j],next[u,s1[i]]);
            end;
        end;

      for i:=1 to l1 do
        if f[l1,i]>l2 then
          begin
            writeln(f2,i);
            exit;
          end;
    end;

procedure init;
    var
      i,now:longint;
      ch:char;
    begin
      for ch:=#1 to #255 do
        begin
          now:=l2+1; next[l2+1,ch]:=l2+1;
          for i:=l2 downto 1 do
            if s2[i]=ch then
              begin
                next[i,ch]:=i;
                now:=i;
              end
            else next[i,ch]:=now;
        end;
    end;

begin
  openF;
  inp;
  init;
  solve;
  closeF;
end.

Download