program wc( input, output );
const MAXPATLEN = 5;
const MAXTEXTLEN = 1000;
const MAXCHAR = 127;
type PATTERN = packed array [1..MAXPATLEN] of char;
TEXT = packed array [1..MAXTEXTLEN] of char;
var pat: PATTERN;
text: TEXT;
pos: integer;
function search( pat: PATTERN; text: TEXT ): integer;
const B = 131;
var hpat, htext, Bm, j, m, n: integer;
found: boolean;
begin
found := FALSE; search := 0;
m := length(pat);
if m=0 then begin
search := 1;
found := TRUE;
end;
Bm := 1;
hpat := 0; htext := 0;
n := length(text);
if n >= m then
for j := 1 to m do begin
Bm := Bm*B;
hpat := hpat*B + ord(pat[j]);
htext := htext*B + ord(text[j]);
end;
writeln( 'pat ', pat, ' text = ', text );
writeln( 'm = ', m, 'n = ', n );
j := m;
while not found do begin
if (hpat = htext) and (pat = substr(text,j-m+1,m)) then begin
search := j-m+1;
found := TRUE;
end;
if j < n then begin
j := j+1;
htext := htext*B - ord(text[j-m])*Bm + ord(text[j]);
end;
end;
end;
begin
{ while not eof(f) do begin read(pat); read(text); }
pos := search('aaaab','aaaa5aaa9aaaab');
writeln( 'pattern found at ', pos );
{ end; }
end.
|