zoukankan      html  css  js  c++  java
  • 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;
    }

  • 相关阅读:
    window 删除文件提示指定的文件名无效或太长
    glib-2.40编译安装
    《Android权威编程指南(The Big Nerd Ranch Guide)(第二版)》12.4挑战练习
    Kotlin中when表达式的使用:超强的switch(KAD 13)
    Kotlin将Realm提升到更高层次
    Kotlin中的“忍者”函数 —— 理解泛型的能力(KAD 12)
    Kotlin中功能操作与集合(KAD 11)
    Kotlin的数据类:节省很多行代码(KAD 10)
    在Android中用Kotlin的Anko运行后台任务(KAD 09)
    Kotlin的扩展函数:扩展Android框架(KAD 08)
  • 原文地址:https://www.cnblogs.com/kkrisen/p/2872938.html
Copyright © 2011-2022 走看看