Bailian2804 词典【map+字典树】

2804:词典
描述
你旅游到了一个国外的城市。那里的人们说的外国语言你不能理解。不过幸运的是,你有一本词典可以帮助你。

输入
首先输入一个词典,词典中包含不超过100000个词条,每个词条占据一行。每一个词条包括一个英文单词和一个外语单词,两个单词之间用一个空格隔开。而且在词典中不会有某个外语单词出现超过两次。词典之后是一个空行,然后给出一个由外语单词组成的文档,文档不超过100000行,而且每行只包括一个外语单词。输入中出现单词只包括小写字母,而且长度不会超过10。
输出
在输出中,你需要把输入文档翻译成英文,每行输出一个英文单词。如果某个外语单词不在词典中,就把这个单词翻译成“eh”。
样例输入
dog ogday
cat atcay
pig igpay
froot ootfray
loops oopslay

atcay
ittenkay
oopslay
样例输出
cat
eh
loops
提示
输入比较大,推荐使用C语言的I / O函数。
来源
翻译自Waterloo local 2001.09.22的试题

问题链接Bailian2804 词典
问题描述:(略)
问题分析
    这个问题与参考链接是同一题。有2种解法,一是使用STL的map,二是使用字典树。用同样的代码,前一种解法程序AC了,而后一种程序出现RE。
程序说明:(略)
参考链接POJ2503 UVA10282 Babelfish【map+字典树】
题记:(略)

AC的C++语言程序如下:

/* POJ2503 UVA1028 Babelfish */  
  
#include <iostream>  
#include <string>  
//#include <unordered_map>  
#include <map>  
#include <sstream>  
  
using namespace std;  
  
int main()  
{  
//    unordered_map<string, string> words;  
    map<string, string> words;  
    string line, first, second;  
    int i;  
  
    while (getline(cin, line)) {  
        if(line.length() == 0)  
            break;  
        istringstream sin(line);  
        sin >> first >> second;  
  
        words[second] = first;  
    }  
  
    while(getline(cin, line)) {  
        i = words.count(line);  
        if (i > 0)  
            cout << words[line] << endl;  
        else  
            cout << "eh" << endl;  
    }  
  
    return 0;  
} 

猜你喜欢

转载自www.cnblogs.com/tigerisland45/p/9998902.html