poj 2503 Babelfish

来源:互联网 发布:迪拜的奇葩法律 知乎 编辑:程序博客网 时间:2024/06/10 14:53
Babelfish
Time Limit: 3000MS Memory Limit: 65536KTotal Submissions: 30041 Accepted: 12974

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.
输入包括多达100,000字典条目,之后是一个空行,接着是长达10万字的消息。每个字典项是一个英文单词跟着一个空格和一个外语单词。外语单词在词典中不会出现一次以上。消息是一段外语,每行一个单词。输入中的每个单词是最多10小写字母的序列。

Output

Output is the message translated to English, one word per line. Foreign words not in the dictionary should be translated as "eh".
输出是翻译成英文,每行一个字的消息。外来语字典中没有应翻译为“en“。

Sample Input

dog ogdaycat atcaypig igpayfroot ootfrayloops oopslayatcayittenkayoopslay

Sample Output

catehloops

Hint

Huge input and output,scanf and printf are recommended.

Source

Waterloo local 2001.09.22
恩。。。基本思路slt,map我不会,使用思路,快排一下,然后二分查找。哈希的思路最强大吧。。。不过没怎么用过。。
#include <iostream>#include <stdio.h>#include <string.h>struct Language{    char main[15];    char local[15];}dict[111111];void qsort(struct Language dict[111111], int p, int r);int partition_q(struct Language dict[111111], int p, int r);int BinSearch(char str[15], int l, int r);int main(){    char str[25];    int i, k;    int flag_foreign = 0;    int dic_num = 0;    int re;    freopen("in.txt", "r", stdin);//    freopen("out.txt", "w", stdout);    while(gets(str)){        if (str[0] == '\0'){            break;        }else{            k = 0; flag_foreign = 0;            for (i = 0;i < strlen(str);i ++){                if (str[i] == ' '){                    dict[dic_num].main[k] = 0;                    flag_foreign = 1;                    k = 0;                    continue;                }                if (flag_foreign){                    dict[dic_num].local[k++] = str[i];                }else{                    dict[dic_num].main[k++] = str[i];                }            }            dict[dic_num].local[k] = 0;            dic_num ++;        }    }//    for (i = 0;i < dic_num;i ++){//        printf("i = %d, main= %s for= %s\n", i, dict[i].main, dict[i].local);//    }//    printf("%d", dic_num);    qsort(dict, 0, dic_num - 1);//    for (i = 0;i < dic_num;i ++){//        printf("i = %d, main= %s for= %s\n", i, dict[i].main, dict[i].local);//    }    while(gets(str)){        if (str[0] == '\0'){            break;        }else{            re = BinSearch(str, 0, dic_num - 1);            if (re >= 0){                printf("%s\n", dict[re].main);            }else if (re < 0){                printf("eh\n");            }        }    }    return 0;}void qsort(struct Language dict[111111], int p, int r){    int q;    if(p < r){        q = partition_q(dict, p, r);        qsort(dict, p, q - 1);        qsort(dict, q + 1, r);    }}int partition_q(struct Language dict[111111], int p, int r){    char x[15];    int i = p - 1, j;    struct Language temp;    strcpy(x, dict[r].local);    for (j = p;j < r;j ++){        if (strcmp(dict[j].local, x) < 0){            i ++;            temp = dict[j]; dict[j] = dict[i]; dict[i] = temp;//            printf("dict[%d] = %s, dict[%d] = %s\n", j, dict[j].local, i, dict[i].local);        }    }    temp = dict[i+1]; dict[i+1] = dict[r]; dict[r] = temp;    return i + 1;}int BinSearch(char str[15], int l, int r){    int mid, num;    while(l <= r){        mid = l + ((r - l) >> 1);        num = strcmp(dict[mid].local, str);        if (num == 0) return mid;        if (num > 0) r = mid - 1;        if (num < 0) l = mid + 1;    }    return -1;}

0 0