Delphi

本类阅读TOP10

·分布式网络考试系统原型分析及实现
·游戏外挂设计技术探讨①
·使用HOOK随心监视Windows
·Delphi 水晶报表打包解决
·试题库开发中非文本数据的处理
·如何将几个DBGRID里的内容导入同一个EXCEL表中....的问题
·如何使用Delphi设计强大的服务器程序
·工人线程中关闭窗体的实现
·用DLL方式封装MDI子窗体。
·支持XP下托盘栏气球提示的托盘单元

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
kmp模式匹配算法的pascal实现

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站

{
  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_nexst( 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[i] = 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.

index函数用于模式匹配,t是模式串,s是原串。返回模式串的位置,找不到则返回0




相关文章

相关软件