zoukankan      html  css  js  c++  java
  • 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] != '
    ') {
    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
    ", p->eng);
    42         else
    43             printf("eh
    ");
    44     }
    45 }
    46 
    47 int main()
    48 {
    49     init();
    50     return 0;
    51 }
    View Code

    灵魂的审视

    注定失败的战争,也要拼尽全力去打赢它; 就算输,也要输得足够漂亮。
  • 相关阅读:
    【PyQt5-Qt Designer】对话框系列
    【PyQt5-Qt Designer】界面布局
    【PyQt5-Qt Designer】PyQt5+eric6 安装和配置
    【PyQt5-Qt Designer】QMessageBox 弹出框总结
    【PyQt5-Qt Designer】鼠标+键盘事件
    【PyQt5-Qt Designer】猜数字(小项目)
    【PyQt5-Qt Designer】浅谈关闭窗口
    【PyQt5-Qt Designer】窗口操作
    【python基础】利用pandas处理Excel数据
    【python基础】os.path模块常用方法详解
  • 原文地址:https://www.cnblogs.com/yalphait/p/10127048.html
Copyright © 2011-2022 走看看