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

    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.
     
     1 #include<cstdio>
     2 #include<cstring>
     3 #include<cstdlib>
     4 typedef struct Trie
     5 {
     6     int num;
     7     struct Trie *next[26];
     8     bool exist;
     9     char ss[11];
    10 }Node,*trie;
    11 Node *create()
    12 {
    13     Node* node=(Node *)malloc(sizeof(Node));
    14     node->num=0;
    15     node->exist=false;
    16     memset(node->next,0,sizeof(node->next));
    17     return node;
    18 }
    19 void insert_(trie root,char *mars,char *en)
    20 {
    21     trie node=root;
    22     char *p=mars;
    23     int id;
    24     while(*p)
    25     {
    26         id=*p-'a';
    27         if(node->next[id]==NULL)
    28         {
    29             node->next[id]=create();
    30         }
    31         node=node->next[id];
    32         ++p;
    33         node->num+=1;
    34     }
    35     node->exist=true;
    36     strcpy(node->ss,en);
    37     //printf("B %s
    ",en);
    38     //printf("A %s
    ",node->ss);
    39 }
    40 char* finds(trie root,char *str)
    41 {
    42 
    43     trie node=root;
    44     char *p=str;
    45     int id;
    46     while(*p)
    47     {
    48         id=*p-'a';
    49         node=node->next[id];
    50         ++p;
    51         if(node==NULL)
    52             return NULL;
    53     }
    54     if(node->exist){
    55             //printf("%s
    ",node->ss);
    56         return node->ss;
    57     }
    58     else
    59         return NULL;
    60 }
    61 int main()
    62 {
    63     trie root=create();
    64     char str1[10],str2[10],*p,ch;
    65     //scanf("%s",str1);
    66     while(scanf("%s",str1))
    67     {
    68         //printf("%s
    ",str1);
    69         scanf("%s",str2);
    70         insert_(root,str2,str1);
    71         getchar();
    72         ch=getchar();
    73         if(ch!='
    ')
    74         {
    75             ungetc(ch,stdin);
    76             continue;
    77         }
    78         break;
    79     }
    80          //printf("%s
    ",finds(root,str1));
    81     while(scanf("%s",str1)!=EOF)
    82     {
    83         p=finds(root,str1);
    84         if(p)
    85             printf("%s
    ",p);
    86         else printf("eh
    ");
    87     }
    88     return 0;
    89 }
  • 相关阅读:
    java中小数的处理:高精度运算用bigDecimal类,精度保留方法,即舍入方式的指定
    java基本类型(数值范围):浮点的底层表示定义,float计算快一些
    Dynamics CRM 开启EmailRouter日志记录
    shell的date日期循环方法:日期格式转时间戳计算,再将时间戳转回日期格式
    shell的数值计算,小数计算
    QT 入门教程经典
    QTSingleApplication使用笔记
    APP 上架苹果应用商城
    C、VDD、VSS、 VEE 和VPP的区别
    PIC12F629帮我用C语言写个程序,控制三个LED亮灭
  • 原文地址:https://www.cnblogs.com/PrayG/p/5899664.html
Copyright © 2011-2022 走看看