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. 连贯接口(fluent interface)的Java实现及应用。

    几年前在单元测试时使用mockito和junit(使用hamcrest提供的比较方法)的时候,就用到过这样类似的语法: mockito: when(mock.someMethod("some ...

  2. Win10/UWP开发—使用Cortana语音指令启动前台App

    这两天进群(53078485)找大咖的童鞋比较多,只是大咖比较忙,目前Demo还没有要到,这里先给大家转载一篇Aran大咖的博客学习下,以下是原文: Win10开发中最具有系统特色的功能点绝对少不了集 ...

  3. Android测试框架初步

    一.实验目的 1.掌握android测试项目的建立 2.掌握android测试框架的基本内容 3.编写运行android测试 二.实验内容与步骤 建立android项目MyProject,运行截图如下 ...

  4. Bootstrap系列 -- 35. 按钮的向下向上三角形

    按钮的向下三角形,我们是通过在<button>标签中添加一个“<span>”标签元素,并且命名为“caret”. <div class="btn-group d ...

  5. Unity3D独立游戏开发日记(二):摆放建筑物

    在沙盒游戏里,能自由建造是很重要的特点,比如说风靡全球的<我的世界>,用一个个方块就能搭建出规模宏大的世界.甚至有偏激的人说,没有自由建造,就不是一个真正的沙盒游戏.的确,沙盒游戏的魅力有 ...

  6. Spring security 和 AOP 学习

    1.Spring security 登录验证拦截器 资源管理拦截器 认证和授权:      认证:登录时候确实存在此用户. 登录要认证!      授权:登录后判断权限级别,然后赋予相应的操作权限. ...

  7. git 命令的学习

    我们在安装好gitlab 之后就是怎么使用它了,这里我选择http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b806 ...

  8. 括号匹配 区间DP (经典)

    描述给你一个字符串,里面只包含"(",")","[","]"四种符号,请问你需要至少添加多少个括号才能使这些括号匹配起来 ...

  9. java 关键字 transient

    一个对象实现了Serilizable 接口,该对象就可以被序列化. 然而在实际开发工程中,我们会遇到,这个类的有些属性不需要序列化,比如包含用户的敏感信息(如密码),为了安全起见,不希望在网络操作(主 ...

  10. Myeclipse报PermGen space异常的问题

    最好用的方法: 1)在myeclipse中windos——>preference——>Myeclipse——>servers——>Tomcat——>JDK(Optiona ...