写的有点烂,凑乎着用var str, Ms, str1: string; maxl, i, j, tmpmax: integer; begin str := 'abcdef123'; str1 := 'xbcyzf123'; for j := 1 to length(str) do begin for i := length(str) downto 1 do begin ms := copy(str, j, i); if pos(ms, str1) > 0 then begin maxl := length(ms); if tmpmax < MAXL then tmpmax := maxl; end; end; end; showmessage(inttostr(tmpmax)); end;
a b c d e f 1 2 3 x - - - - - - - - - b - * - - - - - - - c - - * - - - - - - y - - - - - - - - - z - - - - - - - - - f - - - - - * - - - 1 - - - - - - * - - 2 - - - - - - - * - 3 - - - - - - - - * 斜着的 * 线最长的就是。
var str, Ms, str1: string; i, j, tmpmax: integer; begin i:=0; str := 'abcdef123'; str1 := 'xbcyzf123'; for j := 0 to Min(length(str),length(str1))-1 do begin if str[j]=str1[j] then begin inc(i); tmpMax:=i; end else if str[j]<>str1[j] then begin tmpMax:=Max(tmpMax,i); i:=0; end end; showmessage(inttostr(tmpmax)); end;
maxl, i, j, tmpmax: integer;
begin
str := 'abcdef123';
str1 := 'xbcyzf123';
for j := 1 to length(str) do
begin
for i := length(str) downto 1 do
begin
ms := copy(str, j, i);
if pos(ms, str1) > 0 then
begin
maxl := length(ms);
if tmpmax < MAXL then
tmpmax := maxl;
end;
end;
end;
showmessage(inttostr(tmpmax));
end;
a b c d e f 1 2 3
x - - - - - - - - -
b - * - - - - - - -
c - - * - - - - - -
y - - - - - - - - -
z - - - - - - - - -
f - - - - - * - - -
1 - - - - - - * - -
2 - - - - - - - * -
3 - - - - - - - - *
斜着的 * 线最长的就是。
i, j, tmpmax: integer;
begin
i:=0;
str := 'abcdef123';
str1 := 'xbcyzf123';
for j := 0 to Min(length(str),length(str1))-1 do
begin
if str[j]=str1[j] then
begin
inc(i);
tmpMax:=i;
end
else if str[j]<>str1[j] then
begin
tmpMax:=Max(tmpMax,i);
i:=0;
end
end;
showmessage(inttostr(tmpmax));
end;