百练 2804 词典 解题报告

1.Link:http://poj.grids.cn/practice/2804/

2.content

总时间限制:
3000ms
内存限制:
65536kB
描述
你旅游到了一个国外的城市。那里的人们说的外国语言你不能理解。不过幸运的是,你有一本词典可以帮助你。
输入
首先输入一个词典,词典中包含不超过100000个词条,每个词条占据一行。每一个词条包括一个英文单词和一个外语单词,两个单词之间用一个 空格隔开。而且在词典中不会有某个外语单词出现超过两次。词典之后是一个空行,然后给出一个由外语单词组成的文档,文档不超过100000行,而且每行只 包括一个外语单词。输入中出现单词只包括小写字母,而且长度不会超过10。
输出
在输出中,你需要把输入文档翻译成英文,每行输出一个英文单词。如果某个外语单词不在词典中,就把这个单词翻译成“eh”。
样例输入
dog ogday
cat atcay
pig igpay
froot ootfray
loops oopslay

atcay
ittenkay
oopslay
样例输出
cat
eh
loops
提示
输入比较大,推荐使用C语言的I / O函数。

3.code

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <algorithm>
 4 
 5 
 6 #define MAXSTR 11
 7 #define MAXWORD 100010
 8 
 9 using namespace std;
10 
11 typedef struct
12 {
13     char foreign[MAXSTR];
14     char english[MAXSTR];
15 }  m_word;
16 
17 m_word m_dic[MAXWORD];
18 
19 int compare(const void* elem1,const void* elem2)
20 {
21     return strcmp(((m_word*)elem1)->foreign,((m_word*)elem2)->foreign);
22 }
23 
24 int main()
25 {
26     int count = 0;
27     
28     char chs[30];
29     
30     while(fgets(chs,29,stdin),chs[0] != '\n')
31     {
32         sscanf(chs,"%s%s",m_dic[count].english,m_dic[count].foreign);
33         count++;
34     }
35     
36     qsort(m_dic,count,sizeof(m_word),compare);
37     
38     
39     m_word tmp;
40     m_word* ans;
41     while(scanf("%s",tmp.foreign) != EOF)
42     {
43         ans = (m_word*)bsearch(&tmp,m_dic,count,sizeof(m_word),compare);
44         
45         if(ans != NULL) printf("%s\n",ans->english);
46         else printf("eh\n");
47     }
48 
49     
50     return 0;
51 }

4.method

(1)fgets+sscanf

(2)qsort+bsearch

原文地址:https://www.cnblogs.com/mobileliker/p/3127571.html