POJ 2503

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)  ab:1

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

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

 

二分程序:

#include
#include
#include
#include
#include
#include
#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
#include
#include
#include
#include
#include
#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

 

你可能感兴趣的:(北邮acm50题题解)