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. app整体搭建环境:tabBar切换不同控制器的封装(自定义导航+自定义uiviewcontroler+系统自带tabbar+自定义tabbarController)

    首先,一个app的搭建环境非常重要.既要实现基本功能,又要考虑后期优化的性能. 现在很多应用不仅仅是系统自带的控制器,由于需求复杂,基本上需要自定义多控制器来管理. 新建一个BasicNavigati ...

  2. Spring Cache和MyBatis的使用

    1.http://www.importnew.com/22757.html    spring chache缓存的使用. 2.http://www.importnew.com/22783.html   ...

  3. 【APUE】Chapter17 Advanced IPC & sign extension & 结构体内存对齐

    17.1 Introduction 这一章主要讲了UNIX Domain Sockets这样的进程间通讯方式,并列举了具体的几个例子. 17.2 UNIX Domain Sockets 这是一种特殊s ...

  4. Bootstrap系列 -- 29. 按钮组

    单个按钮在Web页面中的运用有时候并不能满足我们的业务需求,常常会看到将多个按钮组合在一起使用,比如富文本编辑器里的一组小图标按钮等 按钮组和下拉菜单组件一样,需要依赖于button.js插件才能正常 ...

  5. css的6中居中的方式

    请先看blog:http://blog.csdn.net/wolinxuebin/article/details/7615098

  6. [BZOJ 1486][HNOI2009]最小圈(二分答案+dfs写的spfa判负环)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1486 分析:容易想到先二分答案x,然后把所有边的权值-x,那么如果图中存在权值和为0的 ...

  7. nnnaaavvv

    <header id="masthead" class="masthead" role="banner"> <h1 cla ...

  8. hdu1255 矩阵的交 线段树+扫描线

    /* 不是叶子节点 ,且cnt=1.注意这里,cnt=1确切的意义是什么, 应该是,可以确定,这个区间被完全覆盖了1次, 而有没有被完全覆盖两次或以上则不知道无法确定,那么怎么怎么办了, 只要加上t[ ...

  9. tuple内部方法

    代码: #tuple内部方法 ac=('a','r','6','d','a','b','b','e') print(dir(ac)) print(ac.count('a')) print(ac.ind ...

  10. BZOJ-3130 费用流 (听题目胡扯丶裸最大流) 二分判定+最大流+实数精度乱搞

    DCrusher爷喜欢A我做的水题,没办法,只能A他做不动的题了.... 3130: [Sdoi2013]费用流 Time Limit: 10 Sec Memory Limit: 128 MBSec ...