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 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.

Source

 
  字典树,经典题,字典翻译
  这道题和 hdu 1075:What Are You Talking About 很像,只不过那道题是翻译句子,这道题是翻译单词。之前做过类似的题做这道题的时候就比较轻松了。
  题意:给你若干个单词以及其对应的翻译作为字典,然后再给你若干单词,根据之前的字典求他们的翻译,若没有,则输出eh。
  思路:将字典中的单词插入到字典树中,在最后一个节点上加上其翻译单词。查找的时候只要找到了这个单词最后这个节点,且这个节点上的存储翻译单词的域不为空说明存在翻译,输出其翻译,如果域为空,则输出“eh”。
  代码
 #include <iostream>
#include <string.h>
#include <stdio.h> using namespace std;
struct Tire{
Tire *next[];
char *trans;
Tire()
{
int i;
for(i=;i<;i++)
next[i] = NULL;
trans = NULL;
}
} root; void Insert(char word[],char trans[])
{
Tire *p = &root;
int i;
for(i=;word[i];i++){
int t = word[i] - 'a';
if(p->next[t]==NULL)
p->next[t] = new Tire;
p = p->next[t];
}
p->trans = new char[];
strcpy(p->trans,trans);
} void Find(char word[]) //查找该单词的翻译并输出,如果没有则输出eh
{
Tire *p = &root;
int i;
for(i=;word[i];i++){
int t = word[i] - 'a';
if(p->next[t]==NULL){
printf("eh\n");
return ;
}
p = p->next[t];
}
if(p->trans!=NULL)
printf("%s\n",p->trans);
else //没找到
printf("eh");
} int main()
{
char str[];
while(gets(str)){
if(str[]=='\0') //检测到空行
break;
char trans[],word[];
sscanf(str,"%s%s",trans,word);
Insert(word,trans);
}
while(scanf("%s",str)!=EOF) //读取要翻译的单词
Find(str);
return ;
}

Freecode : www.cnblogs.com/yym2013

poj 2503:Babelfish(字典树,经典题,字典翻译)的更多相关文章

  1. poj 2503 Babelfish(Map、Hash、字典树)

    题目链接:http://poj.org/bbs?problem_id=2503 思路分析: 题目数据数据量为10^5, 为查找问题,使用Hash或Map等查找树可以解决,也可以使用字典树查找. 代码( ...

  2. 字典树模板题(统计难题 HDU - 1251)

    https://vjudge.net/problem/HDU-1251 标准的字典树模板题: 也注意一下输入方法: #include<iostream> #include<cstdi ...

  3. CH 1601 - 前缀统计 - [字典树模板题]

    题目链接:传送门 描述给定 $N$ 个字符串 $S_1,S_2,\cdots,S_N$,接下来进行 $M$ 次询问,每次询问给定一个字符串 $T$,求 $S_1 \sim S_N$ 中有多少个字符串是 ...

  4. HDU 1251 统计难题(字典树模板题)

    http://acm.hdu.edu.cn/showproblem.php?pid=1251 题意:给出一些单词,然后有多次询问,每次输出以该单词为前缀的单词的数量. 思路: 字典树入门题. #inc ...

  5. hdu1305 字典树水题

    题意:      给你一些字符串,然后问你他们中有没有一个串是另一个串的前缀. 思路:       字典树水题,(这种水题如果数据不大(这个题目不知道大不大,题目没说估计不大),hash下也行,把每个 ...

  6. 字典树&&01字典树专题&&对字典树的理解

    对于字典树和01字典树的一点理解: 首先,字典树建树的过程就是按照每个数的前缀来的,如果你要存储一个全小写字母字符串,那么这个树每一个节点最多26个节点,这样的话,如果要找特定的单词的话,按照建树的方 ...

  7. poj 2503 Babelfish(字典树或着STL)

    Babelfish Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 35828   Accepted: 15320 Descr ...

  8. poj 2503 Babelfish(字典树或map或哈希或排序二分)

    输入若干组对应关系,然后输入应该单词,输出对应的单词,如果没有对应的输出eh 此题的做法非常多,很多人用了字典树,还有有用hash的,也有用了排序加二分的(感觉这种方法时间效率最差了),这里我参考了M ...

  9. POJ 2503 Babelfish(map,字典树,快排+二分,hash)

    题意:先构造一个词典,然后输入外文单词,输出相应的英语单词. 这道题有4种方法可以做: 1.map 2.字典树 3.快排+二分 4.hash表 参考博客:[解题报告]POJ_2503 字典树,MAP ...

随机推荐

  1. net-snmp的dateandtime数据类型

    net-snmp的dateandtime数据类型 2015/06/12 16:35:59 DateAndTime是Snmpv2中的一种数据类型,它主要提供了对日期时间的描述. 在开发一个snmp相关程 ...

  2. Python自动化之异常处理

    1 异常基本结构 try: pass except Exception,ex: pass 2 异常种类 AttributeError 试图访问一个对象没有的树形,比如foo.x,但是foo没有属性x ...

  3. Android Xpose Hook(一)

    实验环境:     Droid4x模拟器 (目前Android版本4.2.2)     Android Studio 1.下载相关工具 XposedInstaller下载 http://repo.xp ...

  4. chkconfig用法 LINUX

    chkconfig用法 有时候为了方便管理,我们常常喜欢在Linux中将之安装为服务,然后就可以使用服务来管理. 但是当我们运行安装服务的命令时候,假设服务名为myservice #chkconfig ...

  5. kill新号专题

    一.在tomcat启动脚本中看到,kill  -3

  6. Binary Tree Upside Down

    Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that ...

  7. Qt ini配置文件读写

    #include <QSettings> void MainWindow::saveIni() { //如果ini文件不存在,创建新ini文件,存在赋值 QSettings *setIni ...

  8. delphi控件属性大全-详解-简介

    http://blog.csdn.net/u011096030/article/details/18716713 button 组件: CAPTION 属性 :用于在按钮上显示文本内容 Cancel ...

  9. 和我一起学python,初识python (life is short ,we need python)

    作者:tobecrazy  出处:http://www.cnblogs.com/tobecrazy 欢迎转载,转载请注明出处.thank you! 由于项目需要(并非因为life is short), ...

  10. 从下拉菜单拖拽一个元素 出来,插入到页面中的app 列表中

    1,实现功能:从下拉菜单拖拽一个元素 出来,插入到页面中的app 列表中 并实现app向后移动一个元素的位置: 2.实现思路: 01.遍历下拉菜单,添加拖拽方法,实现位置移动功能: 02.遍历app列 ...