博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj 2503:Babelfish(字典树,经典题,字典翻译)
阅读量:6855 次
发布时间:2019-06-26

本文共 2659 字,大约阅读时间需要 8 分钟。

Babelfish

Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 30816   Accepted: 13283

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 ogdaycat atcaypig igpayfroot ootfrayloops oopslayatcayittenkayoopslay

Sample Output

catehloops

Hint

Huge input and output,scanf and printf are recommended.

Source


 
  字典树,经典题,字典翻译
  这道题和  很像,只不过那道题是翻译句子,这道题是翻译单词。之前做过类似的题做这道题的时候就比较轻松了。
  题意:给你若干个单词以及其对应的翻译作为字典,然后再给你若干单词,根据之前的字典求他们的翻译,若没有,则输出eh。
  思路:将字典中的单词插入到字典树中,在最后一个节点上加上其翻译单词。查找的时候只要找到了这个单词最后这个节点,且这个节点上的存储翻译单词的域不为空说明存在翻译,输出其翻译,如果域为空,则输出“eh”。
  代码
1 #include 
2 #include
3 #include
4 5 using namespace std; 6 struct Tire{ 7 Tire *next[26]; 8 char *trans; 9 Tire()10 {11 int i;12 for(i=0;i<26;i++)13 next[i] = NULL;14 trans = NULL;15 }16 } root;17 18 void Insert(char word[],char trans[])19 {20 Tire *p = &root;21 int i;22 for(i=0;word[i];i++){23 int t = word[i] - 'a';24 if(p->next[t]==NULL)25 p->next[t] = new Tire;26 p = p->next[t];27 }28 p->trans = new char[11];29 strcpy(p->trans,trans);30 }31 32 void Find(char word[]) //查找该单词的翻译并输出,如果没有则输出eh33 {34 Tire *p = &root;35 int i;36 for(i=0;word[i];i++){37 int t = word[i] - 'a';38 if(p->next[t]==NULL){39 printf("eh\n");40 return ;41 }42 p = p->next[t];43 }44 if(p->trans!=NULL)45 printf("%s\n",p->trans);46 else //没找到47 printf("eh");48 }49 50 int main()51 {52 char str[11];53 while(gets(str)){54 if(str[0]=='\0') //检测到空行55 break;56 char trans[11],word[11];57 sscanf(str,"%s%s",trans,word);58 Insert(word,trans);59 }60 while(scanf("%s",str)!=EOF) //读取要翻译的单词61 Find(str);62 return 0;63 }

 

Freecode :

转载地址:http://wlyyl.baihongyu.com/

你可能感兴趣的文章
深入理解Java虚拟机阅读心得(二)
查看>>
大数据学习一般学什么
查看>>
sysbench测试mysql的QPS值
查看>>
基于AWS的电子商务网站架构——营销与推荐服务 ...
查看>>
新手上云
查看>>
mysql数据库使用insert语句插入中文数据报错
查看>>
案例-站狼云品智美站助力必信空调中国制造领先品牌
查看>>
ubuntu修改mysql的大小写不敏感
查看>>
为什么屠呦呦获得了诺贝尔奖却没被评上中科院院士?
查看>>
Hibernate使用注释
查看>>
Docker Hub 公有镜像在国内拉取加速配置
查看>>
vue-04
查看>>
直播平台制作中的直播间礼物功能开发基本介绍
查看>>
解密 | 阿里云破图像识别世界纪录的背后
查看>>
阿里云 Aliplayer高级功能介绍(四):直播时移
查看>>
linux less
查看>>
Python 学习(四)
查看>>
SUSE 开发者提议在 GCC 编译器中用 Python 替代 AWK
查看>>
0034-CM启动报InnoDB engine not found分析
查看>>
从奇葩说学到的解题方法
查看>>