您的位置:首页 > 其它

POJ_2503_Babelfish_(Trie/map)

2016-05-13 17:48 260 查看

描述

 

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

 

Waterloo local 2001.09.22

 

 

分析

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

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

 

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

 Trie:

#include <iostream>
#include <cstdio>
#include <string>
#include <map>
using namespace std;

char c[25],a[15],b[15];
map <string,string> m;

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

map
View Code

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: