poj 2503.Babelfish

#include<stdio.h>
#include<string.h>

const int MAX_ENTRY=100000;
const int Max_WORD_LEN=12;
const int MAX_HASH_SIZE=21169;

struct Entry{
	char english[Max_WORD_LEN];
	char foreign[Max_WORD_LEN];
};
struct LNode{
	int ind;
	LNode* next;
};

Entry dict[MAX_ENTRY];
LNode* hashList[MAX_HASH_SIZE];

int ELFHash(char* str);

int main()
{
	char buffer[512];
	LNode* p;
	
	int i=0;
	while(gets(buffer)){
		if(sscanf(buffer,"%s%s",dict[i].english,dict[i].foreign)!=2){
			break;
		}
		else{
			int key=ELFHash(dict[i].foreign);
			
			p=new LNode();
			p->ind=i;
			p->next=hashList[key];
			hashList[key]=p;
			i++;
		}
	}
	
	while(gets(buffer)!=NULL){
		int key=ELFHash(buffer);
		p=hashList[key];
		while(p!=NULL){
			if(	strcmp(buffer,dict[p->ind].foreign)==0){
				break;
			}
			p=p->next;
		}
		
		if(p==NULL){
			printf("eh\n");
		}
		else{
			printf("%s\n",dict[p->ind].english);
		}
	}
	return 0;
}

int ELFHash(char* str)
{
	unsigned long h=0;
	while(*str!='\0'){
		h=(h<<4)+(*str);
		str++;
		unsigned long g=h&0xf0000000L;
		if(g!=0){
			h^=g>>24;
		}
		h&=~g;
	}
	return h%MAX_HASH_SIZE;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值