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 }
  • 相关阅读:
    HTTP 协议详解
    SQL中Group By的使用
    转mysql 多表 update sql语句总结
    数据库SQL优化大总结之 百万级数据库优化方案
    PHP中include和require的区别详解
    【奇怪的知识二】:部分测试名词解释
    【python】UI自动化测试浏览器内部命令模拟F12
    【python】pip设置永久阿里云镜像源
    【奇怪的知识一】:网页缓存清理
    【python】脚本输出接口json数据为表格
  • 原文地址:https://www.cnblogs.com/PrayG/p/5899664.html
Copyright © 2011-2022 走看看