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. storm如何分配任务和负载均衡?

    背景 在上篇:storm的基础框架分析 基本探讨了storm的: worker.executor等组件的关系. 线程模型和消息系统. 任务分配流程. topology提交到执行的过程. 但,感觉对ni ...

  2. 第三章 Js变量的作用域和匿名函数

    3.1 先看下面的事例: ①var temp=0; ②temp=0; 当js解析器检测到①这种情况的时候,解析器会为这个变量开辟一个内存空间,如果前面已经存在这个变量,就会把这个变量覆盖掉. 当解析器 ...

  3. sql server 2008 数据库的完整性约束

    一.数据库完整性概述   1.数据库的完整性:   ①数据库的完整性是指数据的正确性和相容性 ②数据库完整性是防止不合语义或不正确的数据进入数据库 ③完整性体现了是否真实地反映现实世界   例:  学 ...

  4. [poj3274]排排站(Hash)

    题目:http://poj.org/problem?id=3274 题目大意:http://www.wikioi.com/problem/1247/ (此题中文版) 分析: 令sum[i][j]表示a ...

  5. UIScrollView和UIPageController

    实现代码: // // ViewController.m // UIpageControl // // Created by dllo on 16/3/10. // Copyright © 2016年 ...

  6. 用CSS做长度超过长度显示‘...’,当鼠标放上时显示全部内容

    <!DOCTYPE html> <html> <head> <meta name="author" content="Yeeku ...

  7. 设计模式之UML类图的常见关系(一)

    本篇会讲解在UML类图中,常见几种关系: 泛化(Generalization),依赖(Dependency),关联(Association),聚合(Aggregation),组合(Compositio ...

  8. JAVA开发之Eclipse常用的快捷键

    Eclipse是我们常用的java开发编辑器,它支持很多有用但又不太为人所知的快捷键组合.通过这些组合快捷键我们可以更加容易的浏览源代码,使得整体的开发效率和质量得到提升.甚至有一次笔者去参加一个IT ...

  9. web.xml中/与/*的区别

    1.拦截"/",可以实现现在很流行的REST风格.很多互联网类型的应用很喜欢这种风格的URL.为了实现REST风格,拦截了所有的请求.同时对*.js,*.jpg等静态文件的访问也就 ...

  10. mvc:resources

    springmvc 配置静态文件 http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mv ...