POJ2503 UVA10282 Babelfish【map+字典树】

509 篇文章 9 订阅
7 篇文章 2 订阅

Babelfish

Time Limit: 3000MSMemory Limit: 65536K
Total Submissions: 46675Accepted: 19560

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.

Source

Waterloo local 2001.09.22

问题链接POJ2503 UVA10282 Babelfish

问题描述:参见上文。

问题分析

  这个问题是一个字典问题,可以用map来实现,也可以用字典树来解决。问题的关键是时间上能否更快。

程序说明

  解法一是使用map:

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

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

  解法二是使用字典树:

  需要对字典树进行简单的改造。

AC的C++语言程序(解法一:map)如下:

/* 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;
}

AC的C++语言程序(解法二:字典树)如下:

/* POJ2503 UVA10282 Babelfish */

#include <iostream>
#include <stdio.h>
#include <string.h>

using namespace std;

const int N = 100000;
const int LEN = 10;
const int SIZE = 26;
const char SCHAR = 'a';


struct Trie {
    int acnt;   // access count
    int childs[SIZE];
    char word[LEN];
    void init()
    {
        acnt = 1;
        memset(childs, 0, sizeof(childs));
        word[0] = '\0';
    }
} trie[N * LEN];
int ncnt;   // Trie Node count
char s[LEN + 1]; // Input
char w[LEN + 1];

void insert(char s[], char w[])
{
    int p = 0;
    for(int i = 0; s[i]; i++) {
        int k = s[i] - SCHAR;
        int child = trie[p].childs[k];
        if(child) {
            trie[child].acnt++;
            p = child;
        } else {
            trie[++ncnt].init();
            if(s[i + 1] == '\0')        // 改造
                strcpy(trie[ncnt].word, w);
            trie[p].childs[k] = ncnt;
            p = ncnt;
        }
    }
}

int query(char s[])
{
    int p = 0;
    for (int i = 0; s[i]; i++) {
        int k = s[i] - SCHAR;
        int child = trie[p].childs[k];
        if (child == 0)
            return 0;
        else
            p = child;
    }
//    return trie[p].acnt;
    return p;
}

int main()
{
    char ss[LEN + LEN + 1];
    while(gets(ss)) {
        if(ss[0] == '\0')
            break;
        sscanf(ss, "%s%s", w, s);
        insert(s, w);
    }

    while(gets(s)) {
        int ans = query(s);
        printf("%s\n", ans ? trie[ans].word : "eh");
    }

    return 0;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值