Babelfish
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 28766 Accepted: 12407

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

字典树变形。。。。贴贴小模板。。。

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

const int MAXN=1000000;

int tot,root,child[MAXN][26],flag[MAXN],cas=1;
char str[20],dict[100100][20];

void Init()
{
    memset(child[1],0,sizeof(child[1]));
    memset(flag,-1,sizeof(flag));
    flag[1]=0;
    root=tot=1;
}

void Insert(const char *str,int num)
{
    int* cur=&root;
    for(const char *p=str;*p;p++)
    {
        cur=&child[*cur][*p-'a'];
        if(*cur==0)
        {
            *cur=tot++;
            memset(child[tot],0,sizeof(child[tot]));
            flag[tot]=-1;
        }
    }
    flag[*cur]=num;
}

int Query(const char* str)
{
    int *cur=&root;
    for(const char* p=str;*p&&~*cur;p++)
    {
        cur=&child[*cur][*p-'a'];
    }
    if(*cur==0) return -1;
    return flag[*cur];
}

int main()
{
    Init(); cas=1; bool first=true;
    char sss[50];
    while(gets(sss))
    {
        int pos=-1,cnt1=0,cnt2=0;
        int len=strlen(sss);
        for(int i=0;i<len;i++)
        {
            if(sss==' ')
            {
                pos=i; break;
            }
        }
        if(pos!=-1)
        {
            for(int i=0;i<pos;i++)
            {
                dict[cas][cnt1++]=sss;
            }
            for(int i=pos+1;i<len;i++)
            {
                    str[cnt2++]=sss;
            }
            Insert(str,cas++);
        }
        else if(pos==-1)
        {
            if(first) {first=false; continue;}
            int t = Query(sss);
            if(t==-1)
                puts("eh");
            else printf("%s\n",dict[t]);
        }
    }
    return 0;
}

* This source code was highlighted by YcdoiT. ( style: Codeblocks )

POJ 2503 Babelfish的更多相关文章

  1. poj 2503 Babelfish (查找 map)

    题目:http://poj.org/problem?id=2503 不知道为什么 poj  的 数据好像不是100000,跟周赛的不一样 2000MS的代码: #include <iostrea ...

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

    题目链接:http://poj.org/bbs?problem_id=2503 思路分析: 题目数据数据量为10^5, 为查找问题,使用Hash或Map等查找树可以解决,也可以使用字典树查找. 代码( ...

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

    Babelfish Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 30816   Accepted: 13283 Descr ...

  4. poj 2503 Babelfish(字典树或着STL)

    Babelfish Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 35828   Accepted: 15320 Descr ...

  5. poj 2503 Babelfish(字典树哈希)

    Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 29059 Accepted: 12565 Description You hav ...

  6. POJ 2503 Babelfish (STL)

    题目链接 Description You have just moved from Waterloo to a big city. The people here speak an incompreh ...

  7. Poj 2503 Babelfish(Map操作)

    一.Description You have just moved from Waterloo to a big city. The people here speak an incomprehens ...

  8. POJ 2503 Babelfish(map,字典树,快排+二分,hash)

    题意:先构造一个词典,然后输入外文单词,输出相应的英语单词. 这道题有4种方法可以做: 1.map 2.字典树 3.快排+二分 4.hash表 参考博客:[解题报告]POJ_2503 字典树,MAP ...

  9. 题解报告:poj 2503 Babelfish(map)

    Description You have just moved from Waterloo to a big city. The people here speak an incomprehensib ...

随机推荐

  1. Jenkins进阶系列之——13修改Jenkins权限控制

    说明:本方法适用于安全矩阵和项目矩阵授权策略的Jenkins. 很多童鞋在使用jenkins的时候忘记配置权限或者权限配置错误,然后各种蛋疼.最近闲着无事,折腾了下.好了,闲话少扯. Jenkins的 ...

  2. mvc Areas注册域常见问题一

    添加Areas主要目的是区分一些不同的业务,避免不同的业务都在同一个Controllers下造成混乱,在MVC项目上右键->添加区域->我添加了HMbolie和PClient两个区域-&g ...

  3. 【MPI学习3】MPI并行程序设计模式:不同通信模式MPI并行程序的设计

    学习了MPI四种通信模式 及其函数用法: (1)标准通信模式:MPI_SEND (2)缓存通信模式:MPI_BSEND (3)同步通信模式:MPI_SSEND (4)就绪通信模式:MPI_RSEND ...

  4. T4模板根据DB生成实体类

    1.前言 为什么会有这篇文章了,最近看到了一些框架,里面要写的代码太多了,故此就想偷懒,要是能写出一个T4模板,在数据库添加表后,根据模板就可以自动生成了类文件了,这样多好,心动不如行动.记得使用T4 ...

  5. Object C学习笔记25-文件管理(一)

    在此篇文章中简单记录一下文件管理,在Object C中NSFileManager用于管理文件已经路径.在Object C中的文件路径可以是相对路径也可以是绝对路径.斜线“/”开头,斜线实际上就是一个目 ...

  6. node的实践(项目一)

    学习一门语言,我们先要学习他的基本的语法,基本的数据类型,基本的数组操作,字符串的操作,然后就是语言的特性,实现共享和降低耦合的方式,然后开始比较高级的学习(所有语言都是一样的),比如说通信方法,tc ...

  7. 【Aaronyang原创】用linq取出一个集合中重复的数据

    文章已经迁移:http://www.ayjs.net/2013/07/69/ 文章已经迁移:http://www.ayjs.net/2013/07/69/ 文章已经迁移:http://www.ayjs ...

  8. HDU 5973 Game of Taking Stones 威佐夫博弈+大数

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5973 Game of Taking Stones Time Limit: 2000/1000 MS ...

  9. zabbix 客户端的安装

    这里我们在客户端安装就是用rpm的安装方式了: 在我使用RPM安装的时候遇到了一个错误 [root@git src]# rpm -ivh http://repo.zabbix.com/zabbix/3 ...

  10. WordPress HOOK机制原理及代码分析

    WordPress强大的插件机制让我们可以自由扩展功能.网上对插件的使用以及开发方法都有大量资料可以查询. 今天我们就分析一下四个主要函数的代码,包括: add_action.do_action.ad ...