POJ-2503 Babelfish 解题报告

本文介绍了一种通过构建散列表或使用二分查找法解决大规模词汇翻译问题的方法。面对庞大的输入输出数据,作者提供了两种高效的算法实现方案:一种是基于散列(hash)的快速查找,另一种则是利用二分法进行有序数组中的搜索。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Description

You have just moved from Waterloo to a big city. The people here speak an incomprehensible dialect of a foreign language. Fortunately, you have a dictionary to help you understand them.

Input

Input consists of up to 100,000 dictionary entries, followed by a blank line, followed by a message of up to 100,000 words. Each dictionary entry is a line containing an English word, followed by a space and a foreign language word. No foreign word appears more than once in the dictionary. The message is a sequence of words in the foreign language, one word on each line. Each word in the input is a sequence of at most 10 lowercase letters.

Output

Output is the message translated to English, one word per line. Foreign words not in the dictionary should be translated as "eh".

Sample Input

dog ogday
cat atcay
pig igpay
froot ootfray
loops oopslay

atcay
ittenkay
oopslay

Sample Output

cat
eh
loops

Hint

Huge input and output,scanf and printf are recommended.

        题目链接:http://poj.org/problem?id=2503
       解法类型:hash或二分
       解题思路:建立单词hash的散列表,然后直接搜索即可。关键是hash值key要处理好,我的做法是每个字母减去其相应的字符值,然后平方,冲突情况会减小很多。这题目用二分法搜索更快,只要300多MS就可以了,我的hash用了1000+,想来应该是我的key建得不是很好吧。
       算法实现:
 hash法:
//STATUS:C++_AC_1016MS_5504K
#include<stdio.h>
#include<string.h>
const int MAXN=100010,MAX_HA=10000;
int judge();
char map1[MAXN][11],map2[MAXN][11],dic[11];
struct NODE
{
	struct NODE(){next=NULL;num=0;};
	int num;
	NODE *next;
}hash[MAX_HA];
int main()
{
//	freopen("in.txt","r",stdin);
	int i,j,key,ok,t;
	for(i=0;i<MAXN;i++)
	{
		scanf("%c",&map1[i][0]);
		if(map1[i][0]=='\n')break;
		scanf("%s%s",&(map1[i][1]),map2[i]);

		for(j=0,key=0;map2[i][j]!='\0';j++){
			t=map2[i][j]-'a';
			key+=t*t;
		}

		NODE *p=&hash[key],*q=new NODE;     //hash表的构建
		for(;p->next!=NULL;p=p->next);
		p->num=i;
		p->next=q;
		q->next=NULL;
		getchar();            //这里要注意scanf输入流的问题
	}

	while(~scanf("%s",dic))
	{
		ok=judge();
		printf("%s\n",ok>=0?map1[ok]:"eh");
	}
	return 0;
}

int judge()
{
	int i,key,ok,t;
	for(i=0,key=0;dic[i]!='\0';i++){
		t=dic[i]-'a';
		key+=t*t;
	}
	NODE *p=&hash[key];          //hash表的查找
	for(;p->next!=NULL;p=p->next){
		ok=strcmp(map2[p->num],dic);
		if(!ok)return p->num;
	}
	return -1;
}

 二分法:
//STATUS:C++_AC_375MS_2316K
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
const int MAXN=100010;
int judge(char *a,int high);
int cmp_char(const void *a,const void *b);
char map[MAXN][2][11];
int main()
{
//	freopen("in.txt","r",stdin);
	int ok,i;
	char dic[22];
	for(i=0,gets(dic);dic[0]!='\0';i++)
	{
		sscanf(dic,"%s%s",map[i][0],map[i][1]);
		gets(dic);
	}

	qsort(map,i,sizeof(char)*22,cmp_char);   //使单词有序

	while(~scanf("%s",dic))
	{
		ok=judge(dic,i);     //二分查找
		printf("%s\n",ok>=0?map[ok][0]:"eh");
	}
	return 0;
}

int judge(char *a,int high)
{
	int low=0,mid,ok;
	while(low<high){              //二分查找
		mid=(low+high)/2;
		ok=strcmp(map[mid][1],a);
		if(!ok)return mid;
		else if(ok>0)high=mid;
		else low=mid+1;
	}
	return -1;
}

int cmp_char(const void *a,const void *b)
{
	return strcmp((char*)a+11,(char*)b+11);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值