POJ2503 Babelfish

问题链接:POJ2503 Babelfish

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

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

这个问题用类unordered_map来编写程序,时间上会更快一些,也更为合理。

AC通过程序如下:

/* POJ2503 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;
}
时间: 2024-08-01 22:39:01

POJ2503 Babelfish的相关文章

POJ2503——Babelfish(map映射+string字符串)

Babelfish DescriptionYou 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.InputInput consists of up to 100,000 diction

POJ2503 Babelfish map或者hash_map

POJ2503 这是一道水题,用Map轻松AC. 不过,可以拿来测一下字符串散列, 毕竟,很多情况下map无法解决的映射问题需要用到字符串散列. 自己生成一个质数, 随便搞一下. #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<string.h> #include<cmath> #include<vector>

poj2503 Babelfish BKDRhash+链式hash

题目链接 题意:给定字符串以及对应的字符串,再给字符串找到对应的字符串,不存在输出"eh". 思路:造模板. /********************************************************* file name: poj2503.cpp author : kereo create time: 2015年04月12日 星期日 17时13分12秒 ******************************************************

POJ2503 Babelfish【map】

题目链接: http://poj.org/problem?id=2503 题目大意: 给你一本字典.字典上每一行为一个英语单词和一个其他国家单词.这样我们就可以通过字典把英语单词 翻译成其他国家单词,也可以将其他国家单词翻译为英语单词了.现在再给你几个外国单词,问:字典中 是否有这个单词的翻译.如果有,就输出翻译,否则,输出"eh". 思路: 这道题其实可以用STL中的map或是字典树来做.map的做法是,建立两个map,一个对应存放翻译,一 个用来判断翻译是否存在.注意输入可以先将一

POJ2503:Babelfish(二分)

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 ent

poj2503(Babelfish)

题目地址:Babelfish 题目大意: 你将要迁徙一个大城市里,但是你们的语言不一样.但是幸运的是你有一本字典,通过你的字典可以翻译外国的语言.每一行先是字典的单词接着是外国语言.  字典输完,给你几个外国语言,输出字典的单词否则输出“eh”. 解题思路: map将每一行的单词mp一个整数,这整数可以是序列号,这样你找到对应的mp数值就很容易输出.注意输入输出即可. 代码: 1 #include <algorithm> 2 #include <iostream> 3 #inclu

【fzoj 2376】「POJ2503」Babelfish

FZOJ题目链接 题目很简单,但是读入是一个难点. 于是我选择了sscanf sscanf sscanf与scanf略有区别. sscanf函数原型如下 int __cdecl sscanf(const char * restrict _Src,const char * restrict _Format,...) 说的直白点,sscanf需要写数据来源,而scanf不需要 例如 sscanf(x,"%s%s",a,b);对比scanf 就多了一个数据来源 其他点 map即使是strin

POJ 2503 Babelfish (Trie树 或 map)

Babelfish Time Limit: 3000MS        Memory Limit: 65536K Total Submissions: 34278        Accepted: 14706 Description You have just moved from Waterloo to a big city. The people here speak an incomprehensible dialect of a foreign language. Fortunately

Kattis - Babelfish

Babelfish 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 100000100000 dictionary