POJ 2503

本文介绍了一项编程挑战,名为Babelfish,旨在通过二分查找或哈希技术将一种外语翻译成英语。挑战包含大量字典条目和待翻译的文本,要求参赛者实现高效的翻译算法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Babelfish

Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 52653 Accepted: 21811

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

二分查找或者哈希。字符串操作不太熟悉,浪费好久。

字符串操作:strcmp(a,b)  a<b:-1  a==b:0  a>b:1

读操作:gets(s)读入字符串到s,读到换行为止。

sscanf(s, "%s%s", words[k].eng, words[k].forei); 以字符串s为标准输入

 

二分程序:

#include <set>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cmath>
#define INF 2e8
#define MAXN 100010
using namespace std;

struct word{
    char eng[11];
    char forei[11];
}words[MAXN];

int cmp(struct word a, struct word b) {
    return strcmp(a.forei, b.forei) < 0;
}

int b_search(int l, int r, char target[]) {
    while (l <= r) {
        int mid = l + (r-l) / 2;
        int t = strcmp(words[mid].forei, target);
        if (t == 0) return mid;
        else if (t > 0) r = mid - 1;
        else l = mid + 1;
    }
    return -1;
}

int main()
{
    char a[11], b[11];
    int k = 0, index;
    char s[25];
    while(gets(s)) {
        if (s[0] == '\0') break;
        sscanf(s, "%s%s", words[k].eng, words[k].forei);
        k++;
    }
    sort(words, words+k, cmp);
    while (gets(s)) {
        index = b_search(0, k, s);
        if (index == -1) {
            printf("eh\n");
        }
        else printf("%s\n", words[index].eng);
    }
    return 0;
}

 

哈希:

#include <set>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cmath>
#define INF 2e8
#define MAXN 100010
using namespace std;

struct Hash{
    char eng[11];
    char forei[11];
    bool used;
    Hash* next;
    Hash() {
        used=false;
        next=NULL;
    }
    Hash(char *f, char *e) {
        strcpy(forei, f);
        strcpy(eng, e);
        used = false;
        next = NULL;
    }
}words[MAXN];

int ELFHash(char *key) {
    unsigned long h = 0, x = 0;
    while(*key) {
        h = (h<<4)+(*key++);
        if ((x = h&0xf0000000L) != 0) {
            h ^= (x>>24);
            h &= ~x;
        }
    }
    return h%MAXN;
}

int main()
{
    char en[11], fn[11];
    char s[25];
    struct Hash* p;
    while (gets(s)) {
        if (s[0] == '\0') break;
        sscanf(s, "%s %s", en, fn);
        int h = ELFHash(fn);
        if (!words[h].used) {
            words[h].used = true;
            strcpy(words[h].eng, en);
            strcpy(words[h].forei, fn);
        }
        else{
            p = &words[h];
            while(p->next != NULL) p = p->next;
            p->next = new Hash(fn, en);
        }
    }
    while (gets(s)) {
        int h = ELFHash(s);
        if (!words[h].used) printf("eh\n");
        else{
            p = &words[h];
            while (p != NULL) {
                if (!strcmp(s, p->forei)){
                    printf("%s\n", p->eng);
                    break;
                }
                else {
                    p = p->next;
                }
            }
            if(p==NULL) printf("eh\n");
        }
    }
    return 0;
}

stl库的map

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值