POJ_2503_Babelfish(map or 字典树)

Babelfish

Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 34816   Accepted: 14908

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”。

分析:首选就想到了map去做,然后AC了。最近在做字典树的题目,于是用字典树也写了一遍。

题目链接:http://poj.org/problem?id=2503

map代码:

#include<map>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int MAX = 27 ;
const int  MAXN = 100000 + 5 ;

map<string,int>m;
int num = 0 , len;
char s[MAXN][MAX],str[MAXN];

int main(){
    //freopen("liuchu.txt","r",stdin);
    while(gets(s[num])&&s[num][0]!='\0'){
        len = strlen(s[num]);
        int first = 0, k = 0;
        for(int i=0;i<len;i++){
            if(first) str[k++]=s[num][i];
            else if(s[num][i]==' '){
                first = 1;
                s[num][i]='\0';
            }
        }
        m[str]=num++;
        memset(str,'\0',sizeof(str));
    }
    while(scanf("%s",str)!=EOF){
        if(m.count(str))
            printf("%s\n",s[m[str]]);
        else
            printf("eh\n");
    }
    return 0;
}

trie代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int MAX = 27 ;
const int MAXN = 100000 + 5 ;
struct trie{
    int point;
    trie *next[MAX];
};

int number = 0 ;
char s[MAXN][MAX],str[MAX];
trie *root=new trie;

void createTrie(char *s,int n){
    trie *p=root,*q;
    int len=strlen(s),pos;
    for(int i=0;i<len;i++){
        pos=s[i]-'a';
        if(p->next[pos]==NULL){
            q=new trie;
            for(int j=0;j<MAX;j++)
                q->next[j]=NULL;
            p->next[pos]=q;
            p=p->next[pos];
        }
        else{
            p=p->next[pos];
        }
    }
    p->point=n;
}

int findTrie(char *s){
    trie *p=root;
    int len=strlen(s),pos;
    for(int i=0;i<len;i++){
        pos=s[i]-'a';
        if(p->next[pos]==NULL)
            return -1;
        p=p->next[pos];
    }
    return p->point;
}

void delTrie(trie *Root){
    for(int i=0;i<MAX;i++){
        if(Root->next[i]!=NULL)
            delTrie(Root->next[i]);
    }
    free(Root);
}

int main(){
    //freopen("liuchu.txt","r",stdin);
    for(int i=0;i<MAX;i++)
        root->next[i]=NULL;
    while(gets(s[number])&&s[number][0]!='\0'){
        int len=strlen(s[number]),k=0 ;
        bool judge=false;
        for(int i=0;i<len;i++){
            if(judge) str[k++]=s[number][i];
            if(s[number][i]==' '){
                s[number][i]='\0';
                judge=true;
            }
        }
        createTrie(str,number);
        number++;
    }
    while(scanf("%s",str)!=EOF){
        int pose=findTrie(str);
        if(pose==-1)
            printf("eh\n");
        else
            printf("%s\n",s[pose]);
    }
    delTrie(root);
    return 0;
}
时间: 2024-10-03 22:47:14

POJ_2503_Babelfish(map or 字典树)的相关文章

HDU 1075 map or 字典树

What Are You Talking About Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/204800 K (Java/Others)Total Submission(s): 12773    Accepted Submission(s): 4069 Problem Description Ignatius is so lucky that he met a Martian yesterday. But

HDU 1075 What Are You Talking About(map或字典树)

What Are You Talking About Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/204800 K (Java/Others)Total Submission(s): 24624    Accepted Submission(s): 8280 Problem Description Ignatius is so lucky that he met a Martian yesterday. But

51nod 1095 Anigram单词【hash/map/排序/字典树】

1095 Anigram单词 基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题  收藏  关注 一个单词a如果通过交换单词中字母的顺序可以得到另外的单词b,那么定义b是a的Anigram,例如单词army和mary互为Anigram.现在给定一个字典,输入Q个单词,从给出的字典中找出这些单词的Anigram. Input 第1行:1个数N,表示字典中单词的数量.(1 <= N <= 10000) 第2 - N + 1行,字典中的单词,单词长度 <= 10

hdu1251 字典树or map

一道字典树的题,不过看起来用map更为简单 传送门 题意: 给出一堆字符串构成一个字典,求字典里以某字符串为前缀的字符串有几个 思路: 输入字符串时把字符串的前缀全部存进map并标记次数 查询时直接输出就可以了 AC代码: 1 #include "stdio.h" 2 #include "map" 3 #include "string" 4 #include "string.h" 5 #include "iostre

HDU 1247 Hat’s Words (字典树 &amp;amp;&amp;amp; map)

分析:一開始是用递归做的,没做出来.于是就换了如今的数组.即,把每个输入的字符串都存入二维数组中,然后创建字典树.输入和创建完成后,開始查找. 事实上一開始就读错题目了,题目要求字符串是由其它两个输入的字符串组成的前后缀,自己根本没有推断前缀是否满足.就直接推断后缀,一直想不通自己哪里错了,非常羞愧,水平还是不行. 查找分为前缀查找和后缀查找.事实上两个函数基本差点儿相同的.以下放代码. #include <cstdio> #include <cstring> #include &

poj 2503 哈希 Map 字典树

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

STL 之map解决 Message Flood(原字典树问题)

Message Flood Time Limit:1500MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Description Well, how do you feel about mobile phone? Your answer would probably be something like that "It's so convenient and benefits people a lot".

ACM学习历程—HDU 4287 Intelligent IME(字典树 || map)

Description We all use cell phone today. And we must be familiar with the intelligent English input method on the cell phone. To be specific, the number buttons may correspond to some English letters respectively, as shown below: 2 : a, b, c    3 : d

ZOJ 3674 Search in the Wiki(字典树 + map + vector)

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4917 题意:每个单词都一些tips单词.先输入n个单词和他们的tips.然后m组查询,每次查询一些单词,按字典序输出这些单词的公有tips.(每个单词都都只包含小写大写字母) 思路:对第i个单词,用vector数组g,g[i]来存这个单词的所有tips.对于所有单词建立字典树,在单词的结尾结点存好该单词的tips在g数组中存的一维下标i.最后用map来计数每组询问中