切换到宽版
  • 6056阅读
  • 3回复

求助:谁能用FP讲一下KMP算法? [复制链接]

上一主题 下一主题
 
只看楼主 倒序阅读 0 发表于: 2006-07-20
谁能用FP讲一下KMP算法?谢谢.(最好写一下程序)
只看该作者 1 发表于: 2006-07-21
拜托大家,为我这个菜鸟详细讲一讲吧!!!!!!!!!!!!!!
离线r134a
只看该作者 2 发表于: 2006-07-28
我也一直在找这个算法......不知道网上有没有?
.


祝大家明年NOIP大获全盛!


.
离线anacreon
只看该作者 3 发表于: 2006-08-02
{
Implementation of KMP Algorithm
}
PROGRAM Impl_KMP;

USES
  CRT;

CONST
  MAX_STRLEN = 255;

VAR
  next       : array [ 1 .. MAX_STRLEN ] of integer;
  str_s, str_t : string;
  int_i     : integer;

Procedure get_next( t : string );
Var
  j, k : integer;
Begin
  j := 1; k := 0;
  while j < Length(t) do
  begin
      if ( k = 0 ) or ( t[j] = t[k] ) then
      begin
          j := j + 1; k := k + 1;
          next[j] := k;
      end
      else k := next[k];
  end;
End;

Function index( s : string; t : string ) : integer;
Var
  i, j : integer;
Begin
  get_next(t);
  index := 0;
  i := 1; j := 1;
  while ( i <= Length(s) ) and ( j <= Length(t) ) do
  begin
      if ( j = 0 ) or ( s = t[j] ) then
      begin
          i := i + 1; j := j + 1;
      end
      else j := next[j];
      if j > Length(t) then index := i - Length(t);
  end;
End;

BEGIN
  ClrScr;
  Write('s = ');
  Readln(str_s);
  Write('t = ');
  Readln(str_t);
  int_i := index( str_s, str_t );
  if int_i <> 0 then
  begin
      Writeln( 'Found ', str_t, ' in ', str_s, ' at ', int_i, '.' );
  end
  else
      Writeln( 'Cannot find ', str_t, ' in ', str_s, '.' );
END.
快速回复
限100 字节
 
上一个 下一个