POJ2503 UVA10282 Babelfish

问题链接POJ2503 UVA10282 Babelfish

问题描述参见上文。

问题分析这个问题只是一个字典问题,自然用map来实现。问题的关键是时间上能否更快。

程序说明本来是想用类unordered_map采用哈希搜索的map来编写程序,编译不支持,只好改为map。

这个问题用类unordered_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;
}


原文地址:https://www.cnblogs.com/tigerisland/p/7564739.html