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.
解法类型: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);
}