POJ 2503.Babelfish

209


题目




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.


题解



这道题的意思就是进行单词翻译

关键点在于:

  • 读入数据
  • 映射

由于数据存在一行两个单词和一行一个单词两种形式,因此,我们再读入时需要区分开。

另一方面,由于需要对单词进行映射,因此我们可以采用map,进行stringstring的映射

最后输出时使用cout来输出(string格式)



如果需要进一步优化时间可以采用trie树


代码



/*
By:OhYee
Github:OhYee
HomePage:http://www.oyohyee.com
Email:oyohyee@oyohyee.com
Blog:http://www.cnblogs.com/ohyee/

かしこいかわいい?
エリーチカ!
要写出来Хорошо的代码哦~
*/

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <map>
using namespace std;

//DEBUG MODE
#define debug 0

//循环
#define REP(n) for(int o=0;o<n;o++)

const int maxn = 100005;
const int maxm = 100;

inline int read_string(char s[]) {
    char c;
    int i = 0;
    //while(!(((c = getchar()) ==' ') || (c >= 'a'&&c <= 'z')))
    //    if(c == EOF)
    //        return 0;
    if((c = getchar())== EOF)
        return 0;
    while((c == ' ') || (c >= 'a'&&c <= 'z')) {
        s[i++] = c;
        c = getchar();
    }
    s[i] = '\0';
    return i;
}

bool Do() {
    char temp[maxm*2];
    map<string,string> dict;
    map<string,string>::iterator it;
    char a[maxm],b[maxm];

    while(read_string(temp)) {
        if(strcmp(temp,"") == 0)
            break;
        sscanf(temp,"%s %s",a,b);
        dict[b] = a;
    }

    while(scanf("\n%s",a) != EOF) {
        cout << (dict.count(a) ? dict[a] : "eh" )<< "\n";
    }
        

    return false;
}

int main() {
    while(Do());
    return 0;
}
发布评论
  • 点击查看/关闭被识别为广告的评论