• 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 }
  • 相关阅读:
    python之bytes和string(转)
    http post请求传文件报错 invalid character '' in numeric literal
    正则表达式匹配从指定字符开始到指定字符结束的字符串
    为什么提倡会议上要多提问?
    4K 显示器突然变模糊了怎么调回来
    快速将 gif 图转成 jpg 图片
    <一>window安装git
    <二>使用git将本地项目上传到gitee
    <一>window安装rabbitmq
    <一>redis安装
  • 原文地址:https://www.cnblogs.com/PrayG/p/5899664.html
Copyright © 2020-2023  润新知