function lev(s, t: string): integer; function min3(a, b, c: integer): integer; // Внутренняя функция минимума от трех переменных var res: integer; begin res:= a; if b < res then res:= b; if c < res then res:= c; min3:= res; end; const cuthalf = 255;// Максимальная длинна строки //(или же заменить на максимальную длину слов) var i, j, m, n: integer; // Начало самой функции по поиску расстояния Левештейна cost: integer; flip: boolean; Res: integer; buf: array[0..((cuthalf*2)-1)] of integer; begin s:=copy(s, 1, cuthalf-1); t:=copy(t, 1, cuthalf-1); m:=length(s); n:=length(t); if m = 0 then Res:=n else if n = 0 then Res:=m else begin flip:=false; for i:=0 to n do buf[i]:=i; for i:=1 to m do begin if flip then buf[0]:=i else buf[cuthalf]:=i; for j:=1 to n do begin if s[i] = t[j] then cost:=0 else cost:=1; if flip then buf[j]:=min3((buf[cuthalf+j]+1), (buf[j-1]+1), (buf[cuthalf+j-1]+cost)) else buf[cuthalf+j]:=min3((buf[j]+1), (buf[cuthalf+j-1]+1), (buf[j-1]+cost)); end; flip:=not flip; end; if flip then Res:=buf[cuthalf+n] else Res:=buf[n]; end; lev:=Res; end;