描述


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. java经验总结二:ORA-08103: 对象不再存在

    问题发生的环境: 在springMvc+mybatis框架中,调用oracle的存储过程时,碰到的一个这样的异常: org.springframework.jdbc.UncategorizedSQLE ...

  2. iOS开发-自动布局篇:史上最牛的自动布局教学!

    转载自:http://www.jianshu.com/p/f6cf9ef451d9 本文我们将提到: aotulayout(手码) VFL aotulayout(Xib) Masonry(第三方框架) ...

  3. UIView的frame和bounds区别

    UIView的frame和bounds区别 iOS中,大家肯定对view和frame都不陌生,我们设置view在父view中的位置和大小时,只需要设置frame就可以了. 可能大家也有查过网上的一些资 ...

  4. 解决IE浏览器IFrame对象内存不释放问题

    最近项目组发现在使用showModalDialog弹出窗体中如果包含IFrame对象,则IFrame对象占用的内存资源在窗体关闭后不会释放.弹出关闭反复多次后,IE浏览器内存占用可超过数百M,严重时I ...

  5. MySQL数据库原理

    我们知道,数据是信息的载体——一种我们约定了如何解释的符号.在计算机系统中,最常见的应该是文本数据.我们用它记录配置信息,写日志,等等.而在应用程序中,按一定的数据结构来组织数据的方式叫做数据库管理系 ...

  6. php重载

    重载 PHP所提供的"重载"(overloading)是指动态地"创建"类属性和方法.我们是通过 魔术方法(magic methods)来实现的. 当调用当前环 ...

  7. php checkbox复选框值的获取与checkbox默认值输出方法

    php获取 checkbox复选框值的方法,checkbox在php读取值时要用数组形式哦,我们读取这些值用php post获取是以一个array形式哦. php获取 checkbox复选框值的方法 ...

  8. netbeans git 配置(ssh方式)

    git出问题了,自己又重新配置了下git. 参考文章: https://netbeans.org/kb/docs/ide/git_zh_CN.html#github

  9. TDirectory.Delete 创建删除目录简单示例

    使用函数: 1.System.IOUtils.TDirectory.CreateDirectory//创建目录 2.System.IOUtils.TDirectory.Exists        // ...

  10. Delphi实用小function

    Write Log // strLog : the log content need write to log file; // strFile: the compliete file path(in ...