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. 文本比较算法三——SUNDAY 算法

    SUNDAY 算法描述: 字符串查找算法中,最著名的两个是KMP算法(Knuth-Morris-Pratt)和BM算法(Boyer-Moore).两个算法在最坏情况下均具有线性的查找时间.但是在实用上 ...

  2. 正在使用广告标识符 (IDFA)

    APP提交审核后,apple方面一直说我使用了IDFA,APP里没有集合任何广告SDK. 怀疑是其他第三方的SDK用了. 检测命令 //在项目的根目录下用终端执行 grep -r advertisin ...

  3. super用法和继承中的构造方法

    当new出来一个对象的时候,  this是只想对象本身. 在存在继承关系时, 在子类中用super表示引用父类中的东西. 子类的构造过程必须调用父类的构造方法. 子类中包含父类,所以子类中一定要先调用 ...

  4. Excel导出的几种方式

    1.html 前台html与js代码(文件:ExportExcelByHtml.aspx): <html xmlns="http://www.w3.org/1999/xhtml&quo ...

  5. linux mysql查看安装信息

    ps -ef|grep mysql root               ?        :: /bin/sh /usr/bin/mysqld_safe --datadir=/var/lib/mys ...

  6. centos7精简版(minimal)killall: command not found

    centos7精简版(minimal)运行killall命令提示 command not found 是由于没有安装psmisc所致 Psmisc软件包包含三个帮助管理/proc目录的程序. 安装下列 ...

  7. c#excel的操作例子

    class MyData//存储行数据 { public List<string> RowData { get; set; } } static void Main(string[] ar ...

  8. AUTOSSH,ssh反向代理

    在本地机器   1)ssh-keygen   2)ls ~/.ssh/   应该有三个文件 id_rsa id_rsa.pub known_hosts   拷贝id_rsa.pub到远程服务器,然后在 ...

  9. iOS AES加密解密实现方法

    使用方法 先导入头文件 #import "NSData+AES.h" //AES测试 //用来密钥 NSString *key = "; //用来发送的原始数据 NSSt ...

  10. Effective C++ -----条款43:学习处理模板化基类内的名称

    可在derived class templates内通过“this->“指涉base class templates内的成员名称,或藉由一个明白写出的”base class资格修饰符”完成.