maxstr.pas 864 B

123456789101112131415161718192021222324252627282930313233343536
  1. const
  2. len1 = 13;
  3. len2 = 9;
  4. A :string = 'Super Ruez';
  5. B :string = 'dprtupzz';
  6. var
  7. i,j:byte;
  8. V : array[0..len2,0..len1] of byte;
  9. ret : string;
  10. begin
  11. Fillchar(V,len1*len2+2,0);
  12. V[1,1] := 0;
  13. if a[1]=b[1] then v[1,1]:=1;
  14. for i:=2 to len2 do if ((v[i-1,1]=1) or (a[1]=b[i])) then v[i,1] := 1
  15. else v[i,1]:=0;
  16. for j:=2 to len1 do if ((v[1,j-1]=1) or (a[j]=b[1])) then v[1,j] := 1
  17. else v[1,j]:=0;
  18. for i:=2 to len2 do
  19. for j:= 2 to len1 do
  20. begin
  21. if (a[j]=b[i]) then v[i,j];
  22. if v[i-1,j] > v[i,j-1] then v[i,j] := v[i-1,j]
  23. else v[i,j] := v[i,j-1];
  24. end;
  25. while v[i,j] <> 0 do
  26. begin
  27. if (v[i-1,j] = v[i,j]-1) and (v[i,j-1]=v[i,j]-1) then insert(a[j],ret,1);
  28. if v[i-1,j] > v[i,j-1] then dec(i) else dec(j);
  29. end;
  30. WriteLn('Max substring: ',ret);
  31. end.