转论坛上的一个方法,原创者quickkeyboard和jinjiazz program AB5000; {$APPTYPE CONSOLE}
const MaxVal = 300000;
const a: array [0..5] of string=('aba','bcd','afa','agwe','aaf','abweb'); b: array [0..5] of string=('aba','bcd','dfe','agwe','aaf','abweb'); var d: array [0..MaxVal-1] of Byte; i,m: Integer; function ELFHash(s: String): Integer; var g, h, i: LongWord; begin h := 0; for i:=1 to Length(s) do begin h := h shl 4 + Ord(s[i]); g := h and $f0000000; if g <> 0 then h := h xor (g shr 24); h := h and (not g); end; Result := h mod MaxVal; end; begin // 清空Hash表 FillChar(d, MaxVal*Sizeof(Byte), 0); // 构造数组a的Hash表
for i:=0 to 5 do inc(d[ELFHash(a[i])]); // 在数组a的Hash表中查找数组b的元素 for i:=0 to 5 do if d[ELFHash(b[i])]<>0 then Write(b[i], ' '); Writeln; Readln; end. 适合大型运算 同时我也想问一下大家pchar类型的数据怎么用此算法找到交集
如果要查找一个字符串是否在一个字符串集合中,就要使用TStringList了,将字符串集合导入TStringList中,然后通过IndexOF属性来判断
我是判断 某个字符串 如 '101a' 是否在一个可能得取值集合中 '201a' '205b' '101a' 101a 是否在 后面得3个之中
再次感谢 我得是 D7
program AB5000;
{$APPTYPE CONSOLE}
const
MaxVal = 300000;
const
a: array [0..5] of string=('aba','bcd','afa','agwe','aaf','abweb');
b: array [0..5] of string=('aba','bcd','dfe','agwe','aaf','abweb');
var
d: array [0..MaxVal-1] of Byte;
i,m: Integer;
function ELFHash(s: String): Integer;
var
g, h, i: LongWord;
begin
h := 0;
for i:=1 to Length(s) do
begin
h := h shl 4 + Ord(s[i]);
g := h and $f0000000;
if g <> 0 then
h := h xor (g shr 24);
h := h and (not g);
end;
Result := h mod MaxVal;
end;
begin
// 清空Hash表
FillChar(d, MaxVal*Sizeof(Byte), 0);
// 构造数组a的Hash表
for i:=0 to 5 do
inc(d[ELFHash(a[i])]);
// 在数组a的Hash表中查找数组b的元素
for i:=0 to 5 do
if d[ELFHash(b[i])]<>0 then
Write(b[i], ' ');
Writeln;
Readln;
end. 适合大型运算
同时我也想问一下大家pchar类型的数据怎么用此算法找到交集
=========================================================设置CaseSensitive属性
var aa:TStringList ;
begin
aa:=TStringList.Create ;
aa.Add('101a');
aa.Add('101A');
aa.Add('500a');
aa.CaseSensitive :=True ; ShowMessage(IntToStr(aa.IndexOf('101A')));end;