简答题
偏难
#7190
词典 你旅游到了一个国外的城市。那里的人们说的外国语言你不能理解。不过幸运的是,你有一本词典可以帮助你。 时间限制:3000 内存限制:65536 输入 首先输…
词典
你旅游到了一个国外的城市。那里的人们说的外国语言你不能理解。不过幸运的是,你有一本词典可以帮助你。
时间限制:3000
内存限制:65536
输入
首先输入一个词典,词典中包含不超过100000个词条,每个词条占据一行。每一个词条包括一个英文单词和一个外语单词,两个单词之间用一个空格隔开。而且在词典中不会有某个外语单词出现超过两次。词典之后是一个空行,然后给出一个由外语单词组成的文档,文档不超过100000行,而且每行只包括一个外语单词。输入中出现单词只包括小写字母,而且长度不会超过10。
输出
在输出中,你需要把输入文档翻译成英文,每行输出一个英文单词。如果某个外语单词不在词典中,就把这个单词翻译成“eh”。
样例输入
dog ogday cat atcay pig igpay froot ootfray loops oopslay atcay ittenkay oopslay
样例输出
cat eh loops
提示
输入比较大,推荐使用C语言的I / O函数。
答案与解析
正确答案:<pre class="brush:cpp;toolbar:false">#include <algorithm>
#include <stdio.h>
#include <string.h>
#define MAXSTR 11
#define MAXWORD 100010
using namespace std;
typedef struct {
char foreign[MAXSTR];
char english[MAXSTR];
} m_word;
m_word m_dic[MAXWORD];
int compare(const void *elem1, const void *elem2) {
return strcmp(((m_word *)elem1)->foreign, ((m_word *)elem2)->foreign);
}
int main() {
int count = 0;
char chs[30];
while (fgets(chs, 29, stdin), chs[0] != '\n') {
sscanf(chs, "%s%s", m_dic[count].english, m_dic[count].foreign);
count++;
}
qsort(m_dic, count, sizeof(m_word), compare);
m_word tmp;
m_word *ans;
while (scanf("%s", tmp.foreign) != EOF) {
ans = (m_word *)bsearch(&tmp, m_dic, count, sizeof(m_word), compare);
if (ans != NULL)
printf("%s\n", ans->english);
else
printf("eh\n");
}
return 0;
}</pre>
这道题暂未补解析。