poj 2503 Babelfish (map,trie 树)

链接:poj 2503

题意:输入 语言A及翻译为语言B的词典,之后再输入语言B的单词,判断是否能从词典中找到,

若能找到,将其翻译为语言A,否则输出“eh”.

思路:这题肯定得先将词典对应语言存起来,但是如果直接暴力找输入的单词是否出现过,必然会TLE

因为单词都是一对一的关系,可以用map实现

当然,trie树是用空间换时间,对于字符串的查找,在时间上有着相当的优势,因此也可以用trie树

注:sscanf函数,从一个字符串中读进与指定格式相符的数据.

map实现:938MS

#include<iostream>
#include<map>
#include<cstdio>
#include<cstring>
#include<string>
using namespace std;
int main()
{
    map<string,string> word;
    map<string,string>::iterator i;
    char s[25],s1[15],s2[15];
    while(gets(s)!=NULL){
        if(strcmp(s,"")==0)
            break;
        sscanf(s,"%s %s",s1,s2);
        word[s2]=s1;
    }
    while(gets(s)!=NULL){
        if((i=word.find(s))!=word.end())
            cout<<i->second<<endl;
        else
            cout<<"eh"<<endl;
    }
    return 0;
}

trie 树实现:422MS

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct stu
{
    struct stu *next[26];
    char s[12];
}node;
node* creat_node()           //创建新节点并初始化
{
    node *p=(node *)malloc(sizeof(node));
    memset(p->next,0,sizeof(p->next));
    return p;
}
void trie_insert(node *p,char *s,char *word)
{
    int i;
    while(*s!='\0'){
        i=*s-'a';
        if(p->next[i]==0)
            p->next[i]=creat_node();
        p=p->next[i];
        s++;
    }
    strcpy(p->s,word);         //将对应单词存起来
}
void trie_search(node *p,char *s)
{
    int i;
    while(*s!='\0'){
        i=*s-'a';
        p=p->next[i];
        if(p==0){
            printf("eh\n");
            return ;
        }
        s++;
    }
    printf("%s\n",p->s);
}
int main()
{
    node *root=NULL;
    char s[25],t[12],word[12];
    root=creat_node();
    while(gets(s)!=NULL){
        if(strcmp(s,"")==0)
            break;
        sscanf(s,"%s %s",word,t);
        trie_insert(root,t,word);
    }
    while(gets(s)!=NULL)
        trie_search(root,s);
    return 0;
}

poj 2503 Babelfish (map,trie 树)

时间: 2024-10-13 12:44:41

poj 2503 Babelfish (map,trie 树)的相关文章

poj 2503:Babelfish(字典树,经典题,字典翻译)

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

poj 2503 Babelfish(Map、Hash、字典树)

题目链接:http://poj.org/bbs?problem_id=2503 思路分析: 题目数据数据量为10^5, 为查找问题,使用Hash或Map等查找树可以解决,也可以使用字典树查找. 代码(Map实现): #include <iostream> #include <sstream> #include <string> #include <map> using namespace std; int main() { char word[15], fo

POJ 2503 Babelfish(字典树)

题目链接:http://poj.org/problem?id=2503 题意:翻译单词,若在词典中找不到则输出eh. 思路:裸的字典树. 代码: #include <iostream> #include <stdio.h> #include <string.h> #include <math.h> #include <algorithm> #include <string> #include <vector> using

poj 2418 Hardwood Species (trie树)

poj   2418   Hardwood Species http://poj.org/problem?id=2418 trie树+dfs 题意: 给你多个单词,问每个单词出现的频率. 方法:通过字典树,将所有单词放入树中,通过dfs遍历(题目要求按ASSIC码顺序输出单词及其频率),dfs可满足 注意:单词中不一定只出现26个英文字母,ASSIC码表共有256个字符 1 #include <stdio.h> 2 #include <string.h> 3 #include &l

POJ训练计划2418_Hardwood Species(Trie树)

解题报告 Tire树. #include <iostream> #include <cstring> #include <cstdio> #include <algorithm> using namespace std; struct node { int v; node *next[256]; }; int cnt=0,q; char ch[100],str1[100]; node *newnode() { node *p=new node; p->

[POJ 1204]Word Puzzles(Trie树暴搜)

Description Word puzzles are usually simple and very entertaining for all ages. They are so entertaining that Pizza-Hut company started using table covers with word puzzles printed on them, possibly with the intent to minimise their client's percepti

[ACM] POJ 2513 Colored Sticks (Trie树,欧拉通路,并查集)

Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Total Submissions: 29736   Accepted: 7843 Description You are given a bunch of wooden sticks. Each endpoint of each stick is colored with some color. Is it possible to align the sticks in a st

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

[ACM] POJ 2418 Hardwood Species (Trie树或map)

Hardwood Species Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 17986   Accepted: 7138 Description Hardwoods are the botanical group of trees that have broad leaves, produce a fruit or nut, and generally go dormant in the winter. Ameri