以下是我根据 LCS 最长公共子串算法实现 C# http://7422.blog.163.com/blog/static/3364459720085179541550/
改写的一段delphi
能执行,运行结果如下:
例如: str1:='中华人民共和国是我家' str2:='中华人民' 能正确返回 4 说明结果正确。
但 如果 我把str2:='共和国'; 则返回 2. 测试结果说明:当字符串2是从第一个字符开始相等则能正确返回最大值。否则返回的最大字符串数将比正确的个数 少1. 我看了半天没有找到原因,望路过的朋友帮忙看看,我到底哪里出了问题。附录上我改的代码:function LCS(str1,str2:widestring):integer;
var
maxtix:array of integer;
i,j:integer;
maxlength,str1num,str2num:integer;
begin
maxLength:=0;
str1num:=Length(str1);
str2num:=Length(str2); if (str1=str2) then
begin
result:=str1num;
exit;
end; if ((str1num=0) or (str2num=0)) then
begin
Result:=0;
Exit;
end; SetLength(maxtix,str1num+1);
for i:=0 to str2num-1 do
begin
for j:=str1num-1 downto 0 do
begin
if (str2[i]=str1[j]) then
begin
if ((i=0) or (j=0)) then maxtix[j]:=1 else maxtix[j] :=maxtix[j-1] + 1;
end else begin
maxtix[j] := 0;
end; if (maxtix[j] > maxLength) then
begin
maxLength := maxtix[j];
end; end; //end of j:=str1num-1 to 0 do end; // end of i:=0 to str2num do Result:=maxLength; end;
改写的一段delphi
能执行,运行结果如下:
例如: str1:='中华人民共和国是我家' str2:='中华人民' 能正确返回 4 说明结果正确。
但 如果 我把str2:='共和国'; 则返回 2. 测试结果说明:当字符串2是从第一个字符开始相等则能正确返回最大值。否则返回的最大字符串数将比正确的个数 少1. 我看了半天没有找到原因,望路过的朋友帮忙看看,我到底哪里出了问题。附录上我改的代码:function LCS(str1,str2:widestring):integer;
var
maxtix:array of integer;
i,j:integer;
maxlength,str1num,str2num:integer;
begin
maxLength:=0;
str1num:=Length(str1);
str2num:=Length(str2); if (str1=str2) then
begin
result:=str1num;
exit;
end; if ((str1num=0) or (str2num=0)) then
begin
Result:=0;
Exit;
end; SetLength(maxtix,str1num+1);
for i:=0 to str2num-1 do
begin
for j:=str1num-1 downto 0 do
begin
if (str2[i]=str1[j]) then
begin
if ((i=0) or (j=0)) then maxtix[j]:=1 else maxtix[j] :=maxtix[j-1] + 1;
end else begin
maxtix[j] := 0;
end; if (maxtix[j] > maxLength) then
begin
maxLength := maxtix[j];
end; end; //end of j:=str1num-1 to 0 do end; // end of i:=0 to str2num do Result:=maxLength; end;
解决方案 »
- 使用BDE连接intebase的数据库,为什么在win2000下面运作正常,在XP下面就运作不起来?
- 怎么把DBGrid里的数据写到Grid里?
- 大家在用ClientDataSet的时候,碰到过这样的问题吗?
- 哪里有实现管家婆或速达界面的控件?(八百里加急)
- 想把微信公众平台接口转成DELPHI的,那位大侠给个思路
- 一个小问题,如何屏蔽隐式调用post()方法?
- 如何用delphi制作一个独立的www服务器?
- 如何编程控制当前IE图像是否显示?
- 请教:初次使用spcomm控件,在接受事件中传递的是一个指针,如何从指针得到字符串?
- 一次滑稽的解决过程
- 请教各位高手,三层数据库中,如何解决一个客户端修改数据后,其他客户端显示的数据自动同步更新?
- RMReport 打开报错
begin
for j:=str1num downto 1 do
begin