您的位置:首页 > 其它

poj2503 Babelfish (hash)

2009-05-14 09:25 375 查看
这是用ELFHash做的版本:

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

struct LINK{

int hash;

struct LINK *next;

}*link[MAX];

unsigned int ELFHash(char *str)

{

unsigned int hash = 0;

unsigned int x = 0;

while (*str)

{

hash = (hash << 4) + (*str++);

if ((x = hash & 0xF0000000L) != 0)

{

hash ^= (x >> 24);

hash &= ~x;

}

}

return (hash % MAX);

}

int main()

{

int i, key, count=0, flag;

char msg[50];

struct LINK *p;

//freopen("input.txt", "r", stdin);

while (gets(msg) && strcmp(msg, "")!=0){

//¶ÁÈëÊý¾Ý

for (i=0;msg[i]!=' ';i++){

entry[count].english[i]=msg[i];

}

entry[count].english[i]='\0';

strcpy(entry[count].foreign, msg+i+1);

//hash´¦Àí

key=ELFHash(entry[count].foreign);

p=(struct LINK *)malloc(sizeof(struct LINK));

p->hash=count;

p->next=link[key];

link[key]=p;

count++;

}

//²éÕÒ

while (gets(msg)){

flag=0;

key=ELFHash(msg);

p=link[key];

while (p){

if (strcmp(msg, entry[p->hash].foreign)==0){

flag=1;

break;

}

p=p->next;

}

if (flag==1){

printf("%s\n", entry[p->hash].english);

}

else{

printf("eh\n");

}

}

//freopen("CON", "r", stdin);

//system("PAUSE");

return 0;

}

Memory:5872K

Time:297MS
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: