【问题标题】:Rabin Karp Algorithm in pascal帕斯卡中的 Rabin Karp 算法
【发布时间】:2015-05-06 09:20:25
【问题描述】:

谁能给我一个函数的源代码 - Rabin Karp 算法 - 在 pascal(免费 pascal 版本)?

【问题讨论】:

  • 没有。对不起。 Stack Overflow 不是“给我代码” 服务...但是,我们可以帮助您解决实施尝试的具体问题。
  • 好吧,我只是想问一下,因为我在任何地方都找不到帕斯卡。

标签: freepascal rabin-karp


【解决方案1】:

很容易找到 GNU Pascal 的函数

适配FPC:

program Project1;

function search (const pat: string; const  Text: string) : integer;
const
  b = 131;
var
  hpat,
  htext,
  Bm,
  j,
  m,
  n     : integer;
  found : Boolean;
begin
  found := False;
  result := 0;
  m := length (pat);
  if m = 0 then
  begin
    result := 1;
    found := true
  end;

  Bm := 1;
  hpat := 0;
  htext := 0;
  n := length (Text);
  if n >= m then
    {*** preprocessing ***}
    for j := 1 to m do
    begin
      Bm := Bm * b;
      hpat := hpat * b + ord (pat[j]);
      htext := htext * b + ord (Text[j])
    end;

  j := m;
  {*** search ***}
  while not found do
  begin
    if (hpat = htext) and (pat = Copy (Text, j - m + 1, m)) then
    begin
      result := j - m;
      found := true
    end;
    if j < n then
    begin
      j := j + 1;
      htext := htext * b - ord (Text[j - m]) * Bm + ord (Text[j])
    end
    else
      found := true;
  end
end;

begin
  writeln(Search('abcde', '0123456abcde'));
  writeln(Search('abcde', '012345678abcde'));
  writeln(Search('abcde', '0123456785785758'));
  readln;
end.

唯一的区别是函数substr()被替换为Copy()。

【讨论】:

  • 您无需说谢谢,只需接受答案即可。这是这里的小游戏......
猜你喜欢
  • 2022-01-10
  • 1970-01-01
  • 2018-04-09
  • 2022-01-18
  • 1970-01-01
  • 2012-04-09
  • 1970-01-01
  • 1970-01-01
  • 2022-08-19
相关资源
最近更新 更多