描述


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. JDK版本过高,导致Eclipse报错

    1.JDK版本如果比较高,而使用的eclipse版本比较低,导致在eclispe中不能识别而报错.   2.点击Attach Source添加rt.jar后,又出现如下错误 3.这样的错误就是由于ec ...

  2. (二)Android 基本控件

    第一节:View 视图组件 Andorid 中的View 视图组件,实现类是android.view.View 类,是绝大多数图形显示类的父类,提供了大量的方法和属性.在View 类下,有很多子类,如 ...

  3. apparmor介绍

    AppArmor AppArmor 类似于selinux ,主要的作用是设置某个可执行程序的访问控制权限,主要区别就在于apparmor是以路径(path)为基础,而selinux以i节点(inode ...

  4. ios UIWebView截获html并修改便签内容

    需求:混合应用UIWebView打开html后,UIWebView有左右滚动条,要去掉左右滚动效果: 方法:通过js截获UIWebView中的html,然后修改html标签内容: 实例代码: 服务器端 ...

  5. C++指针学习笔记

    本文参考http://www.prglab.com/cms/pages/c-tutorial/advanced-data/pointers.php 1.存储其它变量地址的变量(如下面例子中的addre ...

  6. IDE开发<LER-Studio>(1)::UI设计

    LER-Studio主要使用Microsoft Ribbon界面(fluentUI),大致为Office 2007 black. 首先创建MFC程序,基于多文档视图,Ribbon界面,基类选择CVie ...

  7. Qt标准对话框之QColorDialog

    Qt中提供了一些标准的对话框,用于实现一些常用的预定义功能,比如本节中将要介绍的颜色对话框——QColorDialog. 在不同的系统平台下,颜色对话框的显示效果可能会有所不同,主要因系统主题风格而异 ...

  8. zend studio 9.0.4 安装破解

    转载于 http://www.geekso.com/ZendStudio9-key 注册破解步骤第一步:如果已经安装过Zend Studio 9.0.4的,请打开Zend Studio 9.0.4,在 ...

  9. RESTheart安装与设置

    作者:Maurizio Turatti, 最后在Feb 25, 2016时被 Andrea Di Cesare更新 安装与设置 1. 快速开始 Docker Vagrant Bare metal 2. ...

  10. 版本控制工具git入门

    版本控制工具的历史 不说了,放张图 两者的区别:集中式需要一个中心服务器放置最新的文件,需要联网操作.分布式可以再不联网的情况下操作,前提要拥有版本库 git安装  略 github注册 略 如何在g ...