• poj 2503 Babelfish(字典树或着STL)


    Babelfish
    Time Limit: 3000MS   Memory Limit: 65536K
    Total Submissions: 35828   Accepted: 15320

    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




    STL相同秒杀 我就怀疑字典树有个鸡毛用,測试例子太少了吧!


    STL:

    #include<iostream>
    #include<sstream>
    #include<algorithm>
    #include<cstdio>
    #include<string.h>
    #include<cctype>
    #include<string>
    #include<cmath>
    #include<vector>
    #include<stack>
    #include<queue>
    #include<map>
    #include<set>
    using namespace std;
    
    int main()
    {
        string str;map<string,string >cnt;
        while(getline(cin,str)&&str[0]!=0)
        {
            int loc=str.find(" ");
            cnt[str.substr(loc+1,str.size()-loc-1)]=str.substr(0,loc);
        }
        while(cin>>str)
        {
            if(cnt.count(str))
                cout<<cnt[str]<<endl;
            else
                cout<<"eh"<<endl;
        }
        return 0;
    }
    

    字典树:

    #include<iostream>
    #include<sstream>
    #include<algorithm>
    #include<cstdio>
    #include<string.h>
    #include<cctype>
    #include<string>
    #include<cmath>
    #include<vector>
    #include<stack>
    #include<queue>
    #include<map>
    #include<set>
    using namespace std;
    typedef struct NodeType
    {
        struct NodeType * child[26];
        char word [11];
        int isWord;
        NodeType()
        {
            memset(child,NULL,sizeof(child));
            isWord=0;
        }
    } Node;
    
    void insertWord(Node *node,char *wo,char *wt)
    {
        int id;
        while(*wt)
        {
            id=*wt-'a';
            if(node->child[id]==NULL)
                node->child[id]=new Node();
            node=node->child[id];
            wt++;
        }
        node->isWord=1;
        strcpy(node->word,wo);
    }
    
    char * searchWord(Node * node,char *wd)
    {
        char *noWord="eh";
        int id;
        while(*wd)
        {
            id=*wd-'a';
            if(node->child[id]==NULL)
                return noWord;
            node=node->child[id];
            wd++;
        }
        if(node->isWord)
            return node->word;
        else
            return noWord;
    }
    
    int main()
    {
        char cnt[40],dict[40],buf[40];
        Node * node=new Node;
        while(gets(buf)&&buf[0]!=0)
        {
            int i=0;
            for(; buf[i]!=32; i++)
                cnt[i]=buf[i];
            cnt[i]=0;
            int j;
            for(++i,j=0; buf[i]; i++,j++)
                dict[j]=buf[i];
            dict[j]=0;
            insertWord(node,cnt,dict);
        }
        while(scanf("%s",cnt)!=EOF)
        {
            char *word = searchWord(node,cnt);
            printf("%s
    ",word);
        }
        return 0;
    }
    


  • 相关阅读:
    bzoj3224 普通平衡树
    bzoj 1067 分情况讨论
    bzoj 1269 bzoj 1507 Splay处理文本信息
    bzoj 2733 Splay 启发式合并,名次树
    bzoj1502 simpson求面积
    b_lq_晚会界面单(线段树维护区间最大值表+预留m个位置)
    a_lc_统计子树中城市之间最大距离(枚举子集 + floyd / 2*dfs 求直径)
    b_lq_城市建设 & 公路修建水题 & 新的开始(虚拟结点+MST)
    b_lg_无线通讯网 & 北极通讯网络(问题转化+kruskal)
    b_lg_搭配购买(并查集+01背包)
  • 原文地址:https://www.cnblogs.com/tlnshuju/p/6801155.html
Copyright © 2020-2023  润新知