zoukankan      html  css  js  c++  java
  • POJ 2503 Babelfish




    Babelfish
    Time Limit: 3000MSMemory Limit: 65536K
    Total Submissions: 28766Accepted: 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==0return -1;
        return flag[*cur];
    }

    int main()
    {
        Init(); cas=1bool 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=falsecontinue;}
                int t = Query(sss);
                if(t==-1)
                    puts("eh");
                else printf("%s ",dict[t]);
            }
        }
        return 0;
    }
    * This source code was highlighted by YcdoiT. ( style: Codeblocks )
  • 相关阅读:
    洛谷P2912 [USACO08OCT]牧场散步Pasture Walking [2017年7月计划 树上问题 01]
    洛谷P1082 同余方程 [2012NOIP提高组D2T1] [2017年6月计划 数论06]
    洛谷P2667 超级质数 [2017年6月计划 数论05]
    洛谷P1965 转圈游戏 [2013NOIP提高组 D1T1][2017年6月计划 数论04]
    洛谷P1595 信封问题
    洛谷P1062 数列 [2017年6月计划 数论03]
    洛谷P2835 刻录光盘 [2017年6月计划 强连通分量02]
    洛谷P2826 [USACO08NOV]光开关Light Switching [2017年6月计划 线段树02]
    【模板】矩阵快速幂 洛谷P2233 [HNOI2002]公交车路线
    【模板】ST表 洛谷P1816 忠诚
  • 原文地址:https://www.cnblogs.com/CKboss/p/3350864.html
Copyright © 2011-2022 走看看