ZeroJudge N764: Babelfish

UVa 10282 – Babelfish

You've just moved from Waterloo to a big city. People here speak a dialect that you find hard to understand. Luckily, you have a dictionary to help you understand them.

Sample Inputs/Outputs

Sample Input(s)Sample Output(s)
The input consists of up to 100,000 dictionary entries, followed by a blank line, and then up to 100,000 words in a message. Each dictionary entry is on a separate line, containing an English word followed by a space and then a foreign word. Foreign words in the dictionary will not be repeated. The message is a series of foreign words, with one word per line. Each word in the input contains at most 10 lowercase letters.The output should translate the message into English, with each word on a separate line. Foreign words not found in the dictionary should be translated to "eh".
dog ogday
cat atcay
pig igpay
froot ootfray
loops oopslay

atcay
ittenkay
oopslay
cat
eh
loops

Thought Process

使用 getline 來收前面的資料,如果 getline 到的資料為 empty(),代表那是空行,就開始收下面的資料。將 getline 到的字串分成 a 和 b,並且宣告一個 Map<string, string>,並將 Map[b] 設定成 a。

When collecting the subsequent data, if Map[received string].empty() is true, it means there's no record, so output "eh". Otherwise, output Map[received string].

Sample Code-ZeroJudge N764: Babelfish

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

int main() {
    cin.sync_with_stdio(0);
    cin.tie(0);
    string str;
    map<string, string>MAP;
    while (getline(cin, str) && !str.empty()) {
        string a, b;
        for (int i = 0; i<str.length(); i++) {
            if (str[i] == ' ') {
                b = str.substr(i+1, str.length());
                break;
            }
            a += str[i];
        }
        MAP[b] = a;
    }
    while (cin >> str) {
        if (MAP[str].empty()) cout << "eh\n";
        else cout << MAP[str] << "\n";
    }
}

//ZeroJudge N764
//Dr. SeanXD

Comments