関数のソース コード ( Rabin Karp アルゴリズム) をパスカル (フリー パスカル バージョン) で提供してもらえますか?
1 に答える
0
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()
です。
于 2015-05-08T03:46:38.840 に答える