QBSTR - Xâu con chung dài nhất

Tác giả: ll931110

Ngôn ngữ: Pascal

Program QBSTR;
        Const
                input  = '';
                output = '';
        Var
                x,y: string;
                m,n: integer;
                  F: array[0..1000,0..1000] of integer;

Procedure init;
          Var
                fi: text;
                 i: integer;
          Begin
                Assign(fi, input);
                        Reset(fi);
                        Readln(fi, x);
                        Readln(fi, y);
                Close(fi);

                m:= length(x);
                n:= length(y);

                Fillchar(F, sizeof(F), 0);
          End;

Function max(x,y: integer): integer;
         Begin
                If x < y then max:= y else max:= x;
         End;

Procedure optimize;
          Var
                i,j: integer;
          Begin
              For i:= 1 to m do
                  For j:= 1 to n do
                        If x[i] = y[j] then F[i,j]:= F[i - 1, j - 1] + 1
                                       else F[i,j]:= max(F[i - 1,j], F[i,j - 1]);
          End;

Procedure printresult;
          Var
                fo: text;
          Begin
                Assign(fo, output);
                        Rewrite(fo);
                        Writeln(fo, F[m,n]);
                Close(fo);
          End;

Begin
        init;
        optimize;
        printresult;
End.

Download