POJ2503:Babelfish(二分)

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

咋看是字典树,但是我在搜二分专题的时候看到的这道题,那么这道题肯定能用二分解决
思路很好想,运用到了sscanf函数

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;

struct node
{
    char s1[20],s2[20];
} a[100005];
int len;
int cmp(node a,node b)
{
    return strcmp(a.s2,b.s2)<0;
}

int main()
{
    len = 0;
    int i,j;
    char str[50];
    while(gets(str))
    {
        if(str[0] == '\0')
        break;
        sscanf(str,"%s%s",a[len].s1,a[len].s2);
        len++;
    }
    sort(a,a+len,cmp);
    while(gets(str))
    {
        int l = 0,r= len-1,mid,flag = 1;
        while(l<=r)
        {
            int mid = (l+r)>>1;
            if(strcmp(str,a[mid].s2)==0)
            {
                printf("%s\n",a[mid].s1);
                flag = 0;
                break;
            }
            else if(strcmp(str,a[mid].s2)<0)
                r = mid-1;
            else
                l = mid+1;
        }
        if(flag)
            printf("eh\n");
    }

    return 0;
}

POJ2503:Babelfish(二分),布布扣,bubuko.com

时间: 2024-10-11 22:14:54

POJ2503:Babelfish(二分)的相关文章

POJ2503 Babelfish

问题链接:POJ2503 Babelfish. 这个问题只是一个字典问题,自然用map来实现.问题的关键是时间上能否更快. 本来是想用类unordered_map(采用哈希搜索的map)来编写程序,编译不支持,只好改为map. 这个问题用类unordered_map来编写程序,时间上会更快一些,也更为合理. AC通过程序如下: /* POJ2503 Babelfish */ #include <iostream> #include <string> //#include <u

POJ2503——Babelfish(map映射+string字符串)

Babelfish DescriptionYou 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.InputInput consists of up to 100,000 diction

POJ2503 Babelfish map或者hash_map

POJ2503 这是一道水题,用Map轻松AC. 不过,可以拿来测一下字符串散列, 毕竟,很多情况下map无法解决的映射问题需要用到字符串散列. 自己生成一个质数, 随便搞一下. #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<string.h> #include<cmath> #include<vector>

Babelfish(二分)

Babelfish Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 37238   Accepted: 15879 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

poj2503 Babelfish BKDRhash+链式hash

题目链接 题意:给定字符串以及对应的字符串,再给字符串找到对应的字符串,不存在输出"eh". 思路:造模板. /********************************************************* file name: poj2503.cpp author : kereo create time: 2015年04月12日 星期日 17时13分12秒 ******************************************************

POJ2503 Babelfish【map】

题目链接: http://poj.org/problem?id=2503 题目大意: 给你一本字典.字典上每一行为一个英语单词和一个其他国家单词.这样我们就可以通过字典把英语单词 翻译成其他国家单词,也可以将其他国家单词翻译为英语单词了.现在再给你几个外国单词,问:字典中 是否有这个单词的翻译.如果有,就输出翻译,否则,输出"eh". 思路: 这道题其实可以用STL中的map或是字典树来做.map的做法是,建立两个map,一个对应存放翻译,一 个用来判断翻译是否存在.注意输入可以先将一

poj2503(Babelfish)

题目地址:Babelfish 题目大意: 你将要迁徙一个大城市里,但是你们的语言不一样.但是幸运的是你有一本字典,通过你的字典可以翻译外国的语言.每一行先是字典的单词接着是外国语言.  字典输完,给你几个外国语言,输出字典的单词否则输出“eh”. 解题思路: map将每一行的单词mp一个整数,这整数可以是序列号,这样你找到对应的mp数值就很容易输出.注意输入输出即可. 代码: 1 #include <algorithm> 2 #include <iostream> 3 #inclu

【fzoj 2376】「POJ2503」Babelfish

FZOJ题目链接 题目很简单,但是读入是一个难点. 于是我选择了sscanf sscanf sscanf与scanf略有区别. sscanf函数原型如下 int __cdecl sscanf(const char * restrict _Src,const char * restrict _Format,...) 说的直白点,sscanf需要写数据来源,而scanf不需要 例如 sscanf(x,"%s%s",a,b);对比scanf 就多了一个数据来源 其他点 map即使是strin

D - Babelfish

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 ent