俩种写法哪个效率更高?

tanbo23456 2009-02-17 04:36:05
目的是为了查找俩个字符串里的最长公共部分

function find(source: pchar; find: pchar): string;
var
source_temp: PChar;
pos, len, current_pos, pos_temp: Integer;
pos_result, len_result: Integer;
I: Integer;
begin
source_temp := source;
current_pos := 0;
pos_temp :=0;
pos_result := 0;
len_result := 0;

while (Ord(source_temp^) <> 0) do
begin
pos_temp := current_pos;
for i := 0 to StrLen(find) do
begin
len := 0;
if (source_temp^ = find[i]) then
begin
pos := i;
while ((source[pos_temp] = find[pos] ) and (Ord(source [pos_temp]) <> 0) and (Ord(find[pos]) <> 0)) do
begin
Inc(pos_temp);
Inc(pos);
Inc(len);
end;

if (len> len_result) then
begin
len_result := len;
pos_result := i;
end;
end;
end;
Inc(current_pos);
Inc(source_temp);
end;

while(0 <>(len_result)) do
begin
Result := Result + find[pos_result];
Inc(pos_result);;
Dec(len_result);
end;
end;



function commanstring(shortstring: pchar; longstring: pchar): string;
var
J, I: Integer;
substring: array[0..255] of Char;
begin
result := '';
j := 0;

ShowMessage(IntToStr(Pos(shortstring, longstring)));
if(Pos(shortstring, longstring) > 0) then //strstr是从大串中找小串,找到后返回子串
Result := shortstring;

for i := strlen(shortstring)-1 downto 1 do //否则,开始循环计算
begin
for j := 0 to strlen(shortstring) - i do
begin
FillChar(substring, SizeOf(substring), #0);
Move(shortstring[j], substring[0], i);
if (substring = 'Edit1') then
Null;
if(Pos(substring, longstring) > 0) then
begin //strstr是从大串中找小串,找到后返回子串
Result := substring;
Exit;
end;
end;
end;

end;

...全文
191 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
yc_8301 2009-02-18
  • 打赏
  • 举报
回复
好像第二种效率能搞一些!
天鸽 2009-02-18
  • 打赏
  • 举报
回复
与字符本身的长短有关系
UndefinedCoder 2009-02-18
  • 打赏
  • 举报
回复
楼主的两种写法我倒觉得第一种更快。第二种循环里有内存的拷贝。
Seamour 2009-02-17
  • 打赏
  • 举报
回复
刚写的,时间复杂度和空间复杂度都是Length(A)*Length(B),输出所有匹配结果到LCSs中,返回值是最长公共子串的长度

function LCStrs(const A, B: string; LCSs: TStrings): Integer;
var
lsA, lsB, m, n, it, iCntA : Integer;
Mtx, OffsetsA : array of Integer;
pA, pB : PChar;
begin
lsA := Length(A)+1;
lsB := Length(B)+1;
Result := 0;
iCntA := 0;
SetLength(Mtx, lsA*lsB);
SetLength(OffsetsA, lsA);
pA := Pointer(A);
pB := Pointer(B);

for m:=lsA-2 downto 0 do
for n:=lsB-2 downto 0 do
begin
if(pA[m]<>pB[n])then Continue;
it := Mtx[(m+1)*lsB + n+1] + 1;
Mtx[m*lsB + n] := it;
if(it>Result)then
begin
Result := it;
iCntA := 1;
OffsetsA[0] := m;
end else
if(it=Result)then
begin
OffsetsA[iCntA] := m;
Inc(iCntA);
end;
end;

if(Assigned(LCSs))then
for m:=iCntA-1 downto 0 do
LCSs.Add(Copy(A, OffsetsA[m]+1, Result));
end;
sanguomi 2009-02-17
  • 打赏
  • 举报
回复
第2种
ddgg18 2009-02-17
  • 打赏
  • 举报
回复
不好意思,看错了。。。。
第二种效率高很多
ddgg18 2009-02-17
  • 打赏
  • 举报
回复
喜欢KMP算法求子串
bdmh 2009-02-17
  • 打赏
  • 举报
回复
longest common substring 最长公共子序列
bdmh 2009-02-17
  • 打赏
  • 举报
回复
http://blog.csdn.net/jiju8484/archive/2008/03/05/2151796.aspx
http://hi.baidu.com/cuifenghui/blog/item/538045341d7db34e241f1473.html
给两个参考,虽然不是delphi的,但能看懂
Seamour 2009-02-17
  • 打赏
  • 举报
回复
这俩方法都不是lcs(longest common substring)问题的典型解法,效率都不高
后缀树解法不好懂或不好翻译的话,那个矩阵的动态规划方法还是挺简单的
wsxcdx 2009-02-17
  • 打赏
  • 举报
回复
板凳!

[Quote=引用 1 楼 bdmh 的回复:]
看似第二种效率高
[/Quote]
bdmh 2009-02-17
  • 打赏
  • 举报
回复
看似第二种效率高

16,748

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 语言基础/算法/系统设计
社区管理员
  • 语言基础/算法/系统设计社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧