zoukankan      html  css  js  c++  java
  • POJ_2503_Babelfish_(Trie/map)

    描述


    http://poj.org/problem?id=2503

    给出一个字典,求翻译,翻译不了输出eh.

    Babelfish
    Time Limit: 3000MS   Memory Limit: 65536K
    Total Submissions: 39335   Accepted: 16797

    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

    分析


    网上看到map可直接做,但我就是想打打Trie模板...

    p.s.据说哈希也能做,但我完全不知道那是啥...

    Trie做法:在每个单词节点存下对应翻译的字符串.

     Trie:

     1 #include <cstdio>
     2 #include <iostream>
     3 #include <cstring>
     4 using namespace std;
     5 
     6 const int type=26;
     7 struct Trie{
     8     struct node{
     9         node* next[type];
    10         bool v;
    11         char word[15];
    12         node(){
    13             v=false;
    14             for(int i=0;i<type;i++) next[i]=NULL;
    15             for(int i=0;i<15;i++) word[i]='';
    16         }
    17     }*root;
    18     Trie(){ root=new node; }
    19     void insert(char *c1,char *c2){
    20         node *o=root;
    21         while(*c2){
    22             int t=*c2-'a';
    23             if(o->next[t]==NULL) o->next[t]=new node;
    24             o=o->next[t];
    25             c2++;
    26         }
    27         o->v=true;
    28         strcpy(o->word,c1);
    29     }
    30     void query(char *c){
    31         node* o=root;
    32         while(*c){
    33             int t=*c-'a';
    34             if(o->next[t]==NULL){
    35                 printf("eh
    ");
    36                 return;
    37             }
    38             o=o->next[t];
    39             c++;
    40         }
    41         if(o->v) printf("%s
    ",o->word);
    42         else printf("eh
    ");
    43     }
    44 }tree;
    45 
    46 int main(){
    47     char c[25],a[15],b[15];
    48     while(cin.getline(c,25)){
    49         if(c[0]=='') break;
    50         sscanf(c,"%s %s",a,b);
    51         tree.insert(a,b);
    52     }
    53     while(cin.getline(c,25)){
    54         if(c[0]=='') break;
    55         tree.query(c);
    56     }
    57     return 0;
    58 }
    59 
    60 Trie
    View Code

    map:

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <string>
     4 #include <map>
     5 using namespace std;
     6 
     7 char c[25],a[15],b[15];
     8 map <string,string> m;
     9 
    10 int main(){
    11     while(cin.getline(c,25)){
    12         if(c[0]=='') break;
    13         sscanf(c,"%s %s",a,b);
    14         m[b]=a;
    15     }
    16     map <string,string> :: iterator it;
    17     while(cin.getline(c,25)){
    18         if(c[0]=='') break;
    19         it=m.find(c);
    20         if(it!=m.end()){
    21             printf("%s
    ",it->second.c_str());
    22         }
    23         else{
    24             printf("eh
    ");
    25         }
    26     }
    27     return 0;
    28 }
    29 
    30 map
    View Code
  • 相关阅读:
    java实验四
    C# 和SQL Server 类型转换
    C# 通过this关键字来扩展方法
    类型转换
    获取屏幕高度,兼容性问题解决
    LinqToDataTable
    jQuery层级元素选择器
    Javascript 常用系统内置函数
    JSON-JQuery常用技巧
    .net 调用SAP RFC函数获取数据的两种方式
  • 原文地址:https://www.cnblogs.com/Sunnie69/p/5490241.html
Copyright © 2011-2022 走看看