poj2503--Babelfish(特里一水)

Babelfish

Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 32988   Accepted: 14189

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

Waterloo local 2001.09.22

输入一个字典。前面一个单词和后面的单词映射。问给出的单词有没有相应的,有输出,没有,输出eh

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
struct node{
    int flag ;
    node *next[27] ;
} *head;
node *getnode()
{
    node *p = new node ;
    int i ;
    for(i = 0 ; i < 27 ; i++)
        p->next[i] = NULL ;
    p->flag = -1 ;
    return p ;
}
void gettree(node *p,char *s,int m)
{
    int i , k , l = strlen(s);
    for(i = 0 ; i < l ; i++)
    {
        k = s[i] - 'a' ;
        if( p->next[k] == NULL )
            p->next[k] = getnode();
        p = p->next[k] ;
    }
    p->flag = m ;
}
int f(node *p,char *s)
{
    int i , k , l = strlen(s) ;
    for(i = 0 ; i < l ; i++)
    {
        k = s[i] - 'a' ;
        if( p->next[k] == NULL )
            return -1 ;
        p = p->next[k] ;
    }
    return p->flag;
}
char s1[110000][12] , s2[110000][12] , s[30] ;
int main()
{
    int i = 0 , j , l , k ;
    head = getnode();
    while(1)
    {
        gets(s);
        if(s[0] == '\0')
            break;
        sscanf(s,"%s %s", s1[i], s2[i]);
        gettree(head,s2[i],i);
        i++ ;
    }
    while(gets(s)!=NULL)
    {
        if(s[0] == '\0')
            break;
        k = f(head,s);
        if(k == -1)
            printf("eh\n");
        else
            printf("%s\n", s1[k]);
    }
    return 0;
}

版权声明:转载请注明出处:http://blog.csdn.net/winddreams

时间: 2024-07-29 05:21:59

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>

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(二分)

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

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

POJ 2503 Babelfish (Trie树 或 map)

Babelfish Time Limit: 3000MS        Memory Limit: 65536K Total Submissions: 34278        Accepted: 14706 Description You have just moved from Waterloo to a big city. The people here speak an incomprehensible dialect of a foreign language. Fortunately