PASCAL implementation of KMP mode matching algorithm

zhaozj2021-02-08  261

{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: = 1; k: = 0; while j

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 = T [J]) THEN BEGIN i: = i 1; J: = J 1; ELSE J: = Next [J]; IF J> Length (T) THEN INDEX: = I - Length (T); end;

Begin Clrs; 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,'. '); ELSE WRITELN (' Cannot Find ', Str_T,' IN ', STR_S,'); END.

The index function is used to match, T is the mode string, and S is the original string. Returns the position of the mode string, if you can't find it, return 0

转载请注明原文地址:https://www.9cbs.com/read-2627.html

New Post(0)