POJ-2503 Babelfish 解题报告
2012-03-20 22:45
351 查看
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法:
二分法:
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); }
相关文章推荐
- POJ-2503 Babelfish 解题报告
- poj 2503 Babelfish(字典树或STL水题) 解题报告(百炼2804)
- Pku acm 2503 Babelfish 查找算法解题报告(一) ----二叉查找数(BST)
- [解题报告]POJ_2503 字典树,MAP
- POJ 2503 解题报告
- POJ2503解题报告
- POJ 2305 解题报告
- POJ - 1961 Period解题报告(kmp next数组巧用)
- poj 1338 ugly numbers和poj 2247Humble Numbers 解题报告(附详细分析)
- POJ 1273 解题报告
- POJ1260 Pearls 解题报告
- POJ1012 Joseph解题报告
- poj 2503 Babelfish
- POJ-1833-排列 解题报告
- Poj 2503 Babelfish(Map操作)
- POJ 2376 解题报告
- poj1504解题报告
- poj 2503 Babelfish
- POJ 2299 解题报告
- POJ 3516 数学期望 解题报告