poj2503 Babelfish (hash)
这是用ELFHash做的版本:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #define MAX 100003
5 struct ENTRY{
6 char foreign[11];
7 char english[11];
8 }entry[MAX];
9
10 struct LINK{
11 int hash;
12 struct LINK *next;
13 }*link[MAX];
14
15 unsigned int ELFHash(char *str)
16 {
17 unsigned int hash = 0;
18 unsigned int x = 0;
19 while (*str)
20 {
21 hash = (hash << 4) + (*str++);
22 if ((x = hash & 0xF0000000L) != 0)
23 {
24 hash ^= (x >> 24);
25 hash &= ~x;
26 }
27 }
28 return (hash % MAX);
29 }
30 int main()
31 {
32 int i, key, count=0, flag;
33 char msg[50];
34 struct LINK *p;
35 //freopen("input.txt", "r", stdin);
36 while (gets(msg) && strcmp(msg, "")!=0){
37 //¶ÁÈëÊý¾Ý
38 for (i=0;msg[i]!=' ';i++){
39 entry[count].english[i]=msg[i];
40 }
41 entry[count].english[i]='\0';
42 strcpy(entry[count].foreign, msg+i+1);
43 //hash´¦Àí
44 key=ELFHash(entry[count].foreign);
45 p=(struct LINK *)malloc(sizeof(struct LINK));
46 p->hash=count;
47 p->next=link[key];
48 link[key]=p;
49 count++;
50 }
51 //²éÕÒ
52 while (gets(msg)){
53 flag=0;
54 key=ELFHash(msg);
55 p=link[key];
56 while (p){
57 if (strcmp(msg, entry[p->hash].foreign)==0){
58 flag=1;
59 break;
60 }
61 p=p->next;
62 }
63 if (flag==1){
64 printf("%s\n", entry[p->hash].english);
65 }
66 else{
67 printf("eh\n");
68 }
69 }
70 //freopen("CON", "r", stdin);
71 //system("PAUSE");
72 return 0;
73 }
74
Memory:5872K
Time:297MS