const
maxlen = 12;
type
strtp = record
ch:Array [1..maxlen] of char;
curlen:0..maxlen
end;
procedure TForm1.get_next(t:strtp; var next:array of integer);
var
j,k: integer;
begin
{求模式串t的next函数值并存入数组next}
j := 1;
k := 0;
next[1] := 0;{初始化}
while j < t.curlen do
begin
if (k=0) or (t.ch[j]=t.ch[k]) then
begin
j := j + 1;
k := k + 1;
next[j] := k;
end
else
k := next[k];
end;
end;
var
t: strtp;
i: integer;
begin
t.ch[1] := 'a';
t.ch[2] := 'a';
t.ch[3] := 'b';
t.ch[4] := 'b';
t.ch[5] := 'a';
t.ch[6] := 'a';
t.ch[7] := 'c';
t.ch[8] := 'c';
t.ch[9] := 'a';
t.ch[10] := 'a';
t.ch[11] := 'b';
t.ch[12] := 'b';
t.curlen := 11;
get_next(t,next);
for i := 1 to 12 do
showmessage(inttostr(next));
我仿照课本上的算法,写了一段程序,怎么报出来的结果和课本上的答案不一样啊。P81页的例子。请看看,我觉得这块很重要的,没准考试的时候就要考呢