• POJ2503(二分搜索)


                                                                                               Babelfish
    Time Limit: 3000MS   Memory Limit: 65536K
    Total Submissions: 26333   Accepted: 11301

    Description

    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

    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

    Output is the message translated to English, one word per line. Foreign words not in the dictionary should be translated as "eh".

    Sample Input

    dog ogday
    cat atcay
    pig igpay
    froot ootfray
    loops oopslay
    
    atcay
    ittenkay
    oopslay
    

    Sample Output

    cat
    eh
    loops
    

    Hint

    Huge input and output,scanf and printf are recommended.

    Source

     
    依然是二分搜索的思想,不过这次,因为数据全部是字符串,所以在比较过程中,用到字符串函数Strcmp,算是字符串与二分搜索的一个集中体现,一开始WA了下,一定要注意每道题检测下特殊值与边界值,比如输入为一个空字符,则依然要输出对应的“eh”。。。而且发现用scanf读字符串特别不靠谱,根本就无法检测出
    输入数据之间的空行,在杨宇大神的指点下,学会了SSCANF函数。膜拜下。。。。。
     
     
     

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    struct diction
    {
    char dic[15];
    char dia[15];
    } data[110000];

    bool cmp(diction x,diction y)
    {
    return (strcmp(x.dia,y.dia)<0);
    }
    int main()
    {
    char ch[15];
    int t=0;
    char s[30];
    while (gets(s)&&s[0]!='\0')
    {
    sscanf(s,"%s%s",data[t].dic,data[t].dia);
    t++;
    }
    std::sort(data,data+t,cmp);
    while (gets(ch))
    {
    int min=0,mid,max=t;
      for(;;)
      {
       mid=(max-min)/2+min;
       if (mid==min) break;
       if (strcmp(data[mid].dia,ch)<=0) min=mid;
        else max=mid;
      }
      if (strcmp(ch,data[mid].dia)==0)
       printf("%s\n",data[mid].dic);
       else puts("eh");
    }
    return 0;
    }

  • 相关阅读:
    mac安装mysql 8.0.20
    leetcode之两数之和
    家人闲坐,灯火可亲汪曾祺散文集读书笔记
    java入门知识代码练习
    苏世民:我的经验与教训读后感
    java入门知识
    创业者日志——易居cms产品有什么不同的地方?
    易优CMS:channelartlist 获取当前频道的下级栏目的内容列表
    房产小程序可以实现什么功能?有什么优势?怎么推广小程序?
    房产中介是否需要用管理系统?哪个房产中介管理软件好?
  • 原文地址:https://www.cnblogs.com/kkrisen/p/2872938.html
Copyright © 2020-2023  润新知