Babelfish(c语言)

您所在的位置:网站首页 离开翻译成英文单词 Babelfish(c语言)

Babelfish(c语言)

2024-06-02 12:33| 来源: 网络整理| 查看: 265

题目描述 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 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 is the message translated to English, one word per line. Foreign words not in the dictionary should be translated as “eh”. 你刚刚从滑铁卢搬到了一个大城市。这里的人说着一种难以理解的外语方言。幸运的是,你有一本字典来帮助你理解它们。 输入 输入由最多 100,000 个词典条目组成,后跟一个空行,后跟最多 100,000 个单词的消息。每个词典条目都是一行,其中包含一个英语单词,后跟一个空格和一个外语单词。在字典中,没有一个外来词出现超过一次。该消息是外语中的一系列单词,每行一个单词。输入中的每个单词都是最多包含 10 个小写字母的序列。 输出 输出是翻译成英语的消息,每行一个单词。不在字典中的外来词应翻译为"eh"。 样例输入 dog ogday cat atcay pig igpay froot ootfray loops oopslay

atcay ittenkay oopslay 样例输出 cat eh loops 提示 Huge input and output,scanf and printf are recommended.

#include #include #include typedef struct { char en[11]; char fn[11]; }dict; dict a[100001]; /* 定义qsort比较函数 */ int q_cmp(const void * a,const void *b) { return strcmp(((dict*)a)->fn, ((dict*)b)->fn); } /* 定义bsearch比较函数 */ int b_cmp(const void* a, const void* b) { return strcmp((char*)a, ((dict*)b)->fn); } int main() { char str[30]; int i, sign; dict *p; i = 0; /* 查询标记记为"未开始" */ sign = 1; /* 读取字符串直到文件结束 */ while(gets(str)) { /* 遇到空行则开始排序字典 */ if (str[0] == '\0') { /* 查询标记记为"开始" */ sign = 0; qsort(a, i, sizeof(dict), q_cmp); continue; } /* 查询未开始时读取字典信息 */ if (sign) { /* 使用sscanf从str中读取查询要求 */ sscanf(str, "%s %s", a[i].en, a[i].fn); i++; } /* 查询开始时进行查询 */ else { /* 二分查找指定字符串 */ p = (dict*) bsearch(str, a, i, sizeof(dict), b_cmp); /* 找到则输出结果 */ if (p) { puts(p->en); } /* 找不到输出"eh" */ else { puts("eh"); } } } return 0; }


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3