zoukankan      html  css  js  c++  java
  • Babelfish 分类: 哈希 2015-08-04 09:25 2人阅读 评论(0) 收藏

    Babelfish
    Time Limit: 3000MS Memory Limit: 65536K
    Total Submissions: 36398 Accepted: 15554

    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.
    一道哈希的题,用字典树过的,不过在输入数据的过程中却卡住了,后来在搜题解知道用sscanf()处理的,见识少啊.

    #include <map>
    #include <list>
    #include <cmath>
    #include <queue>
    #include <stack>
    #include <string>
    #include <cstdio>
    #include <cstring>
    #include <cstdlib>
    #include <iostream>
    #include <algorithm>
    #define LL long long
    #define eps 1e-9
    #define PI acos(-1.0)
    #define INF 0x3f3f3f3f
    #define CRR fclose(stdin)
    #define CWW fclose(stdout)
    #define WW freopen("output.txt","w",stdout)
    #define RR freopen("input.txt","r",stdin)
    
    using namespace std;
    
    const int MAX = 100010;
    
    char s[MAX][15],c[15];
    
    struct node
    {
        int  flag;
        node *next[26];
    }*head;
    int top;
    node * Creat()
    {
        node *p;
        p=new node;
        p->flag=0;
        for(int i=0;i<26;i++)
        {
            p->next[i]=NULL;
        }
        return p;
    }
    void Build_Tree()
    {
        node *p=head;
        int a,len=strlen(c);
        for(int i=0;i<len;i++)
        {
            a=c[i]-'a';
            if(!p->next[a])
            {
                p->next[a]=Creat();
            }
            p=p->next[a];
        }
        p->flag=top;//将对应单词的编号赋给它,便于查找
    }
    int Look()
    {
        node *p=head;
        int a,len=strlen(c);
        for(int i=0;i<len;i++)
        {
            a=c[i]-'a';
            if(!p->next[a])
            {
                return 0;
            }
            p=p->next[a];
        }
        if(!p->flag)//查找不到返回零
        {
            return 0;
        }
        else
        {
            return p->flag;
        }
    }
    int main()
    {
        head=Creat();
        top=1;
        int t;
        char cc[30];
        while(gets(cc)&&cc[0]!='')
        {
    
            sscanf(cc,"%s %s",s[top],c);
            Build_Tree();
            top++;
    
        }
        while(~scanf("%s",c))
        {
            t=Look();
            if(t)
            {
                printf("%s
    ",s[t]);
            }
            else
            {
                printf("eh
    ");
            }
        }
        return 0;
    }
    

    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    [BZOJ 1012][JSOI2008]最大数maxnumber(线段树)
    [BZOJ 1011][HNOI2008]遥远的行星(奇技淫巧)
    [BZOJ 1010][HNOI2008]玩具装箱toy(斜率优化Dp)
    [HDU 3507]Print Article(斜率优化Dp)
    [BZOJ 1006][HNOI2008]神奇的国度(MCS弦图的染色)
    [ZOJ 1015]Fishing Net(MCS弦图的判定)
    进程的状态及转换
    程序、进程、线程的概念与比较
    ES6 模块化规范
    DNS域名解析过程(详细)
  • 原文地址:https://www.cnblogs.com/juechen/p/4721932.html
Copyright © 2011-2022 走看看