Pattern matching machine


program wc( input, output ); const MAXPATLEN = 5; const MAXTEXTLEN = 1000; type PATTERN = packed array [1..MAXPATLEN] of char; TEXT = packed array [1..MAXTEXTLEN] of char; var pat: PATTERN; text: TEXT; i: integer; function search( k: integer; pat: PATTERN; text: TEXT ): integer; var i, j, m, n, count: integer; found: boolean; begin writeln('pat: ', pat, 'text: ', text ); found := FALSE; search := 0; m := length(pat); if m=0 then begin search := 1; found := TRUE; end; n := length(text); writeln( 'm = ', m, 'n = ', n ); j := 1; i := 1; while (i<=n-m+1) and not found do begin count := 0; j := 1; while (j <= m) and (count <= k) do begin if text[i+j-1] <> pat[j] then count := count + 1; j := j + 1; end; if count <= k then begin search := i; found := TRUE; end; i := i + 1; end end; begin { while not eof(f) do begin read(pat); read(text); } i := search(1,'aaaab','aa3a5a7a9aaaab'); writeln( 'pattern found at ', i ); { end; } end.

Pascal source (718.wc.p)



© Addison-Wesley Publishing Co. Inc.