18.12.16 词典(排序+二分查找)

描述

你旅游到了一个国外的城市。那里的人们说的外国语言你不能理解。不过幸运的是,你有一本词典可以帮助你。

输入首先输入一个词典,词典中包含不超过100000个词条,每个词条占据一行。每一个词条包括一个英文单词和一个外语单词,两个单词之间用一个空格隔开。而且在词典中不会有某个外语单词出现超过两次。词典之后是一个空行,然后给出一个由外语单词组成的文档,文档不超过100000行,而且每行只包括一个外语单词。输入中出现单词只包括小写字母,而且长度不会超过10。输出在输出中,你需要把输入文档翻译成英文,每行输出一个英文单词。如果某个外语单词不在词典中,就把这个单词翻译成“eh”。

样例输入

dog ogday
cat atcay
pig igpay
froot ootfray
loops oopslay

atcay
ittenkay
oopslay

样例输出

cat
eh
loops

提示

输入比较大,推荐使用C语言的I / O函数。来源翻译自Waterloo local 2001.09.22的试题

题解

 1 #include <iostream>
 2 #include <string.h>
 3 #include <algorithm>
 4 #include <stack>
 5 #include <string>
 6 #include <math.h>
 7 #include <queue>
 8 #include <stdio.h>
 9 #include <string.h>
10 #include <vector>
11 #include <fstream>
12 #define maxn 100005
13 #define inf 999999
14 #define cha 127
15 using namespace std;
16 
17 struct node {
18     char eng[12];
19     char fore[12];
20 }dic[maxn];
21 
22 int cmp(const void* a,const void* b) {
23     return strcmp(((node*)a)->fore, ((node*)b)->fore);
24 }
25 int search(const void*a, const void*b) {
26     return strcmp((char*)a, ((node*)b)->fore);
27 }
28 
29 void init() {
30     char eng[30], fore[18];
31     int count = 0;
32     while (fgets(eng, 29, stdin) && eng[0] != '\n') {
33         sscanf(eng, "%s%s", dic[count].eng, dic[count].fore);
34         count++;
35     }
36     qsort(dic, count, sizeof(node), cmp);
37     while(scanf("%s", fore) != EOF) {
38         node*p = NULL;
39         p = (node*)bsearch(fore, dic, count, sizeof(node), search);
40         if (p)
41             printf("%s\n", p->eng);
42         else
43             printf("eh\n");
44     }
45 }
46 
47 int main()
48 {
49     init();
50     return 0;
51 }
View Code

灵魂的审视

转载于:https://www.cnblogs.com/yalphait/p/10127048.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值