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 dbgrid查询内容的导出为txt函数

Delphi base64, quoted-printable 的解码与编码函数

Delphi nethttpclient post 函数

Delphi 字符串前后翻转

行为判断Delphi中窗体最大化与最小化事件

Delphi经常用到的公共代码(tools.pas)

Delphi 得到文件创建时间,修改时间,访问时间

Delphi区分汉字和字母的函数

Delphi 鼠标拖动控件自由移动位置

shellexecute()和winexec()区别

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



打赏

取消

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

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

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

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

评论

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