Помогите плииииизз!!! Хотел написать прогу для поиска фрагментов строк по шаблону в тексте, с помощью алгоритма Бойера-Мура. Вот чё получилось:
| Цитата | unit Unit1;
interface
uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;
type TForm1 = class(TForm) btn1: TButton; mmo1: TMemo; mmo2: TMemo; edt1: TEdit; lbl1: TLabel; procedure btn1Click(Sender: TObject); private { Private declarations } public { Public declarations } end;
var Form1: TForm1;
implementation type TIntVect = array [0..255] of Integer; TBMTable = array [0..0] of TIntVect; PBMTable = ^TBMTable; function WCBeginsWith( const P, S : String) : Boolean;
var i, lp : Integer; begin Result := False; lp := Length(P); if lp > Length(S) then Exit; for i := 1 to lp do if (P[i]<>S[i]) and (P[i]<>'?') and (S[i]<>'?') then Exit; Result := True; end; function WCFindRightmost( const S, P : String; l : Integer) : Integer; var i, j, lp : Integer; begin Result := 0; lp := Length(P); if lp > l then Exit; for i := l - lp + 1 downto 1 do for j := 1 to lp do if (P[j]<>S[i+j-1]) and (P[j]<>'?') and (S[i+j-1]<>'?') then Break else if j = lp then begin Result := i; Exit; end; end; procedure WCMakeBMTable( var BMT : PBMTable; const P : String); var i, j, lp, MaxShift, CurShift, SufPos : Integer; Suffix : String; begin lp := Length(P); GetMem(BMT, SizeOf(TIntVect)*lp); if P[lp] = '?' then for i := 0 to 255 do BMT^[lp-1][i] := 0 else begin for i := 0 to 255 do BMT^[lp-1][i] := lp; for i := lp downto 1 do if BMT^[lp-1][Byte(P[i])] = lp then BMT^[lp-1][Byte(P[i])] := lp - i; end; MaxShift := lp; for i := lp - 1 downto 1 do begin SetLength(Suffix, lp - i); Move(P[i+1], Suffix[1], lp - i); if WCBeginsWith(Suffix, P) then MaxShift := i; if P[i] = '?' then for j := 0 to 255 do BMT^[i-1][j] := 0 else for j := 0 to 255 do begin CurShift := MaxShift; SetLength(Suffix, lp - i + 1); Suffix[1] := Char(j); Move(P[i + 1], Suffix[2], lp - i ); SufPos := WCFindRightmost(P, Suffix, lp - 1); if SufPos <> 0 then CurShift := i - SufPos; BMT^[i-1][j] := CurShift; end; BMT^[i-1][Byte(P[i])] := 0; end; end; function BMSearch( StartPos, lp : Integer; const S : String; BMT : PBMTable) : Integer; var Pos, i : Integer; begin Pos := StartPos + lp -1; while Pos < Length(S) do for i := lp downto 1 do if BMT^[i-1][Byte(S[Pos-lp+i])] <> 0 then begin Pos := Pos + BMT^[i-1][Byte(S[Pos-lp+i])]; Break; end else if i = 1 then begin bmsearch := Pos - lp + 1;
Exit; end; bmsearch := 0;
end; {$R *.dfm} procedure TForm1.btn1Click(Sender: TObject); var s,p: string; i: integer; begin s:=mmo1.text; p:= edt1.text;
mmo2.Lines.Add(inttostr(BMSearch(1,s,p)));
end; end.
|
В стоке «mmo2.Lines.Add(inttostr(BMSearch(1,s,p)));» выдаёт такую ошибку: «Incompatinle types: Integer and String» |