您的位置:首页 > 其它

POJ 2503 Babelfish(HASH)

2010-08-06 01:38 351 查看
//字典HASH简单应用,这题也可用Trie做
//学会了SSCANF的使用,难点在于处理数据的读入
#include<iostream>
using namespace std;
const int MAXHASH = 100003;
int head[MAXHASH],next[MAXHASH];
int m;
struct Dic
{
char english[11];
char foreign[11];
}dic[MAXHASH];
int hash(char *str)//ELFHash函数,黑书刘汝佳推荐使用……
{
int h = 0;
while(*str)
{
h = (h << 4) + *str++;
int g = h & 0xf0000000L;
if(g) h ^= g >> 24;
h &= ~g;
}
return h % MAXHASH;
}
void insert(char english[],char foreign[])
{
strcpy(dic[m].english,english);
strcpy(dic[m].foreign,foreign);
int h = hash(foreign);
next[m] = head[h];
head[h] = m++;
}
int Find(char foreign[])
{
int h = hash(foreign);
for(int e = head[h];e != -1;e = next[e])
{
if(strcmp(dic[e].foreign,foreign) == 0)
return e;
}
return -1;
}
int main()
{
char buff[30];
char english[11],foreign[11],str[11];
m = 0;
memset(head,-1,sizeof(head));
while(gets(buff) && buff[0] != 0)//用get读整行,buff[0] == 0表示这行是换行
{
sscanf(buff,"%s %s",english,foreign);
insert(english,foreign);
}
while(scanf("%s",str) != EOF)
{
int ans = Find(str);
if(ans >= 0)	printf("%s/n",dic[ans].english);
else printf("eh/n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: