[POJ2503 Babelfish]

[题目来源]:POJ2503

[关键字]:hash

[题目大意]:给出一个字典,和一些待查询的单词,要求查处所有单词,没有则输出‘eh’。

//===========================================================================================================

[分析]:可以将整个字典进行字符串hash,然后查找。

[代码]:(题目代码+各种字符串Hash)

POJ2503
 1 program Project1;
2 type
3 rec = record
4 o, t: string[11];
5 end;
6 var
7 num: array[0..20000] of longint;
8 h: array[0..20000,0..10] of rec;
9
10 function hash(s: string):longint;
11 var
12 seek, i: longint;
13 begin
14 seek := 31;
15 hash := 0;
16 for i := 1 to length(s) do
17 hash := (hash*seek+ord(s[i])) and $FFFFFFFF;
18 hash := hash and $7FFFFFFF;
19 end;
20
21 procedure init;
22 var
23 k, i: longint;
24 s, s1: string;
25 f: boolean;
26 begin
27 readln(s);
28 while s <> '' do
29 begin
30 s1 := copy(s,1,pos(' ',s)-1);
31 delete(s,1,pos(' ',s));
32 k := hash(s) mod 15889;
33 //writeln(k);
34 inc(num[k]);
35 h[k,num[k]].o := s;
36 h[k,num[k]].t := s1;
37 readln(s);
38 end;
39
40 while not seekeof do
41 begin
42 readln(s);
43 k := hash(s) mod 15889;
44 f := false;
45 for i := 1 to num[k] do
46 if h[k,i].o = s then
47 begin
48 writeln(h[k,i].t);
49 f := true;
50 break;
51 end;
52 if not f then writeln('eh');
53 end;
54 end;
55
56 begin
57 init;
58 end.
RS Hash Function
 1 function RSHash(s1:string):dword;        //qword used;
2 var
3 a:qword; //unsigned int a = 63689;
4 b:dword;
5 hash:qword; //unsigned int hash = 0;
6 i:integer;
7 begin
8 a:=63689;
9 b:=378551;
10 hash:=0;
11
12 for i:=1 to length(s1) do
13 begin
14 hash:=(hash*a+ord(s1[i])) and $FFFFFFFF;
15 a:=(a*b) and $FFFFFFFF;
16 end;
17
18 RSHash:=hash and $7FFFFFFF;
19 end;
JS Hash Function
 1 function JSHash(s1:string):dword;        //qword used;
2 var
3 i:integer;
4 hash:qword;
5 begin
6 hash:=1315423911;
7 for i:=1 to length(s1) do
8 hash:=(hash xor ((hash shl 5)+ord(s1[i])+(hash shr 2))) and $FFFFFFFF;
9 JSHash:=hash and $7FFFFFFF;
10 end;
ELF Hash Function
 1    function ELFHash(s1:string):dword;
2 var
3 i:integer;
4 x:dword;
5 begin
6 ELFHash:=0;
7 x:=0;
8 for i:=1 to length(s1) do
9 begin
10 ELFHash:=(ELFHash shl 4)+ord(s1[i]);
11 x:=ELFHash and $F0000000; //x = hash & 0xF0000000L
12 if x<>0 then
13 begin
14 ELFHash:=ELFHash xor (x shr 24);
15 ELFHash:=ELFHash and not x;
16 end;
17 end;
18 ELFHash:=ELFHash and $7FFFFFFF;
19 end;
BKDR Hash Function
 1 function BKDRHash(s1:string):dword;      //qword used;
2 var
3 i:integer;
4 seed:dword;
5 hash:qword;
6 begin
7 seed:=131; // 31 131 1313 13131 131313 etc..
8 hash:=0;
9 for i:=1 to length(s1) do
10 hash:=(hash*seed+ord(s1[i])) and $FFFFFFFF;
11 BKDRHash:=hash and $7FFFFFFF;
12 end;
DJB Hash Function
 1 function DJBHash(s1:string):dword;       //qword used;
2 var
3 i:integer;
4 hash:qword;
5 begin
6 hash:=5381;
7 for i:=1 to length(s1) do
8 hash:=(hash+(hash shl 5)+ord(s1[i])) and $FFFFFFFF;
9 DJBHash:=hash and $7FFFFFFF;
10 end;
AP Hash Function
 1 function APHash(s1:string):dword;
2 var
3 i:integer;
4 begin
5 APHash:=0;
6 for i:=1 to length(s1) do
7 if i and 1=1 then
8 APHash:=APHash xor ((APHash shl 7) xor ord(s1[i]) xor (APHash shr 3))
9 else
10 APHash:=APHash xor (not ((APHash shl 11) xor ord(s1[i]) xor (APHash shr 5)));
11 APHash:=APHash and $7FFFFFFF;
12 end;
SDBM Hash Function
 1 function SDBMHash(s1:string):dword;      //qword used;
2 var
3 i:integer;
4 hash:qword;
5 begin
6 hash:=0;
7 for i:=1 to length(s1) do
8 hash:=(ord(s1[i])+(hash shl 6)+(hash shl 16)-hash) and $FFFFFFFF;
9 SDBMHash:=hash and $7FFFFFFF;
10 end;

除题目代码为本人所为,其余皆出自:http://hi.baidu.com/satily/blog/item/5f5ac3fcdaae09fbfc037fcb.html

你可能感兴趣的:(poj)