NKH - Tách Từ

Tác giả: RR

Ngôn ngữ: Pascal

//Written by Nguyen Thanh Trung
{$R+,Q+}
{$Mode objFPC}
uses math;
const
  FINP          =       '';
  FOUT          =       '';
  MAXN          =       222;
var
  f1,f2         :       text;
  l,l1,l2       :       longint;
  s,s1,s2       :       string;
  f             :       array[1..MAXN,1..MAXN,1..MAXN] of boolean;

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);
      readln(f1,s);  l :=length(s );
    end;
procedure solve;
    var
      i,j1,j2:longint;
    begin
      fillchar(f,sizeof(f),false);
      f[l+1,l1+1,l2+1]:=true;
      for i:=l downto 1 do
      for j1:=l1+1 downto 1 do
      for j2:=l2+1 downto 1 do
        f[i,j1,j2]:= (f[i+1,j1+1,j2] and (s[i]=s1[j1]))
                  or (f[i+1,j1,j2+1] and (s[i]=s2[j2]));
    end;
procedure ans;
    var
      i,j1,j2:longint;
    begin
      j1:=1; j2:=1;
      for i:=1 to l do
        if (s1[j1]=s[i]) and f[i+1,j1+1,j2] then
          begin
            write(f2,'1');
            inc(j1);
          end
        else
          begin
            write(f2,'2');
            inc(j2);
          end;
    end;

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

Download