unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 =
class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Button1: TButton;
Label1: TLabel;
Label2: TLabel;
Button2: TButton;
Button3: TButton;
Button4: TButton;
Label3: TLabel;
Edit3: TEdit;
procedure Button1Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function knuth_morris_pratt(
const target, pattern : PChar;
const lTarget, lPattern : integer) : integer;
var
step :
array[0..255]
of integer;
i,
j : integer;
begin
result := -1;
if ltarget * lpattern = 0
then exit;
i := 0;
j := -1;
step[0] := -1;
repeat
if (j = -1)
or (pattern[i] = pattern[j])
then
begin
inc(i);
inc(j);
if pattern[j] = pattern[i]
then step[i] := step[j]
else step[i] := j;
end else j := step [j];
until i = lpattern -1;
j := -1;
i := 0;
while i < ltarget
do
begin
if (j=-1)
or (pattern[j] = target[i])
then
begin
inc(i);
inc(j);
if j >= lpattern
then
begin
result := i-j+1;
exit;
end;
end else j := step[j];
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var i, t:integer; s1,s2:
string;
begin
t := gettickcount;
s1:=edit1.Text;
s2:=edit2.Text;
i:=knuth_morris_pratt(pchar(s1),pchar(s2),length(s1),length(s2));
label2.Caption:=inttostr(i);
Edit3.Text := floattostrf((gettickcount-t)*0.001,fffixed,10,2);
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
edit1.Text:= '
';
edit2.Text:= '
';
label2.Caption:= '
';
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
close;
end;
procedure TForm1.Button4Click(Sender: TObject);
begin
edit1.text:= '
hallo liebe info freune! info mit frau müller macht sp.-spa.-sp (ach scheiß drauf, macht auf jeden fall iwas)';
edit2.Text:= '
frau müller';
end;
end.