描述


http://poj.org/problem?id=2503

给出一个字典,求翻译,翻译不了输出eh.

Babelfish
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 39335   Accepted: 16797

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

分析


网上看到map可直接做,但我就是想打打Trie模板...

p.s.据说哈希也能做,但我完全不知道那是啥...

Trie做法:在每个单词节点存下对应翻译的字符串.

Trie:

 #include <cstdio>
#include <iostream>
#include <cstring>
using namespace std; const int type=;
struct Trie{
struct node{
node* next[type];
bool v;
char word[];
node(){
v=false;
for(int i=;i<type;i++) next[i]=NULL;
for(int i=;i<;i++) word[i]='\0';
}
}*root;
Trie(){ root=new node; }
void insert(char *c1,char *c2){
node *o=root;
while(*c2){
int t=*c2-'a';
if(o->next[t]==NULL) o->next[t]=new node;
o=o->next[t];
c2++;
}
o->v=true;
strcpy(o->word,c1);
}
void query(char *c){
node* o=root;
while(*c){
int t=*c-'a';
if(o->next[t]==NULL){
printf("eh\n");
return;
}
o=o->next[t];
c++;
}
if(o->v) printf("%s\n",o->word);
else printf("eh\n");
}
}tree; int main(){
char c[],a[],b[];
while(cin.getline(c,)){
if(c[]=='\0') break;
sscanf(c,"%s %s",a,b);
tree.insert(a,b);
}
while(cin.getline(c,)){
if(c[]=='\0') break;
tree.query(c);
}
return ;
} Trie

map:

 #include <iostream>
#include <cstdio>
#include <string>
#include <map>
using namespace std; char c[],a[],b[];
map <string,string> m; int main(){
while(cin.getline(c,)){
if(c[]=='\0') break;
sscanf(c,"%s %s",a,b);
m[b]=a;
}
map <string,string> :: iterator it;
while(cin.getline(c,)){
if(c[]=='\0') break;
it=m.find(c);
if(it!=m.end()){
printf("%s\n",it->second.c_str());
}
else{
printf("eh\n");
}
}
return ;
} map

POJ_2503_Babelfish_(Trie/map)的更多相关文章

  1. BZOJ 2754 [SCOI2012]喵星球上的点名 (AC自动机+map维护Trie树)

    题目大意:略 由于字符集大,要用map维护Trie树 并不能用AC自动机的Trie图优化,不然内存会炸 所以我用AC自动机暴跳fail水过的 显然根据喵星人建AC自动机是不行的,所以要根据问题建 然而 ...

  2. 数据结构 - trie

    #include <cstring> #include <iostream> #include <map> #include <cstdio> usin ...

  3. trie字典树模板浅析

    什么是trie? 百度百科 又称单词查找树,Trie树,是一种树形结构,是一种哈希树的变种.典型应用是用于统计,排序和保存大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计.它的 ...

  4. 布隆过滤器(Bloom Filter)的原理和实现

    什么情况下需要布隆过滤器? 先来看几个比较常见的例子 字处理软件中,需要检查一个英语单词是否拼写正确 在 FBI,一个嫌疑人的名字是否已经在嫌疑名单上 在网络爬虫里,一个网址是否被访问过 yahoo, ...

  5. Bloom Filter布隆过滤器原理和实现(1)

    引子 <数学之美>介绍布隆过滤器非常经典: 在日常生活中,包括设计计算机软件时,经常要判断一个元素是否在一个集合中.比如: 在字处理软件中,需要检查一个英语单词是否拼写正确(也就是要判断它 ...

  6. [GitHub] 75+的 C# 数据结构和算法实现

    C#中标准数据结构和算法的即插即用类库项目 GitHub:https://github.com/aalhour/C-Sharp-Algorithms Watch: 307 Star: 3.4k For ...

  7. [ACM] POJ 2418 Hardwood Species (Trie树或map)

    Hardwood Species Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 17986   Accepted: 713 ...

  8. 【一题多解】 map 二分 hash trie poj 2503

    各种方式解这道题!! 利用map 超时了 #include <iostream> #include <string> #include <map> using na ...

  9. hdu 1251:统计难题[【trie树】||【map】

    <题目链接> 统计难题                        Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131 ...

随机推荐

  1. Find Minimum in Rotated Sorted Array问题的困惑

    今天做了两题,第二题没解出来,发现太麻烦了,放弃了……明天脑子清楚的时候再做. 第一题就是标题中的这个问题.在一个旋转排序数组中找出最小的值. 针对该问题出了两道不同要求的题目,分别是不考虑重复元素的 ...

  2. windows2008 x86 安装 32位oracle

    1.windows 2008 升级到sp2补丁 下载地址 : http://www.microsoft.com/zh-cn/download/confirmation.aspx?id=15278 2. ...

  3. [Twisted] Test

    由于Twisted程序采用事件驱动,并使用Deferred来处理事件,使用Python unittest的写测试并不容易.因此, Twisted拓展了unitest,并使用命令行工具来运行测试.这些组 ...

  4. Apache配置命令

    Apache的主配置文件: 1.DocumentRoot——指定网站的根目录 提示:该目录必须存在.目录上不能有汉字或空格. 2.DirectoryIndex (1)描述:设置网站的默认首页文件.访问 ...

  5. 05_Excel操作_01_简单导入导出

    [Excel组成] 主要由四部分组成: 1.工作簿 每一个Excel文件都可以看成是一个工作簿,当打开一个Excel文件时,相当于打开了一个Excel工作簿. 2.工作表 当打开了Excel工作簿后, ...

  6. [翻译][MVC 5 + EF 6] 1:创建数据模型

    原文:Getting Started with Entity Framework 6 Code First using MVC 5 1.新建MVC项目: 2.修改Views\Shared\_Layou ...

  7. (转)myrepo

    源作者主页:https://copr.fedoraproject.org/coprs/mosquito/myrepo/ 源作者github: https://github.com/1dot75cm/m ...

  8. 深入了解absolute

    1.absolute与float的相同的特性表现  a.包裹性  b.破坏性:父元素没有设置高或宽,父元素的高或宽取决于这个元素的内容  c.不能同时存在 2.absolute独立使用,不与relat ...

  9. 在 lamp(centos)下配置二级 域名 、虚拟主机

    1.你得拥有一个泛域名解析的顶级域名,有一个独立的IP: 2.解析二级域名,如在万网中心里,记录类型为A, 主机记录即为要配的二级域名(如:增加两个:bbs.mydomain.com 和 www.my ...

  10. textarea 超过字数

    <textarea name="></textarea> <div id="statementRowChk"></div> ...