Delphi实现的Sunday字符串搜索算法


本文整理自网络,侵删。

 

type
  TBMKey=record
    Length:Integer;
    Offsets:array[0..255] of Integer;
    ps:PAnsiChar;
  end;
procedure InitKeyDistance(var AKey:TBMKey);
var
  I:Integer;
begin
AKey.Length:=StrLen(AKey.ps);
for I := 0 to 255 do
  AKey.Offsets[I]:=AKey.Length;
for I := 0 to AKey.Length-1 do
  AKey.Offsets[Ord(AKey.ps[I])]:=AKey.Length-I-1;
end;
function SundayStrStrA(s:PAnsiChar;const Key:TBMKey):PAnsiChar;
var
  i,pos,ls:Integer;
begin
if s=nil then
  Result:=nil
else if Key.ps=nil then
  Result:=s
else
  begin
  ls:=StrLen(s);
  Result:=nil;
  pos:=1;
  while pos<=ls-Key.Length do
    begin
    I:=pos-1;
    while I-pos+1<Key.Length do
      begin
      if s[I]<>Key.ps[I-pos+1] then
        Break;
      Inc(I);
      end;
    if i-pos+1=Key.Length then
      begin
      Result:=s+pos-1;
      Break;
      end
    else
      Inc(pos,Key.Offsets[Ord(s[pos+Key.Length-1])]+1);
    end;
  end;
end;

相关阅读 >>

Delphi android 获取通�记录

Delphi 清空dbgrd和stringgrid

Delphi 实现类似windows的查找功能-遍历整个硬盘目录

win7下使用Delphi7的方法

Delphi获取当前计算机所有盘符

Delphi判断ie是否使用代理

Delphi 如何获取桌面图标方法

Delphi spcomm 接收数据不完整

Delphi 打开win8及以上操作系统的系统已安装程序目录

Delphi 获取计算机从开机开始的已运行时间

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...