zoukankan      html  css  js  c++  java
  • HDU 1075 What Are You Talking About

    Description

    Ignatius is so lucky that he met a Martian yesterday. But he didn't know the language the Martians use. The Martian gives him a history book of Mars and a dictionary when it leaves. Now Ignatius want to translate the history book into English. Can you help him?

    Input

    The problem has only one test case, the test case consists of two parts, the dictionary part and the book part. The dictionary part starts with a single line contains a string "START", this string should be ignored, then some lines follow, each line contains two strings, the first one is a word in English, the second one is the corresponding word in Martian's language. A line with a single string "END" indicates the end of the directory part, and this string should be ignored. The book part starts with a single line contains a string "START", this string should be ignored, then an article written in Martian's language. You should translate the article into English with the dictionary. If you find the word in the dictionary you should translate it and write the new word into your translation, if you can't find the word in the dictionary you do not have to translate it, and just copy the old word to your translation. Space(' '), tab(' '), enter(' ') and all the punctuation should not be translated. A line with a single string "END" indicates the end of the book part, and that's also the end of the input. All the words are in the lowercase, and each word will contain at most 10 characters, and each line will contain at most 3000 characters.

    Output

    In this problem, you have to output the translation of the history book.

    Sample Input

    START
    from fiwo
    hello difh
    mars riwosf
    earth fnnvk
    like fiiwj
    END
    START
    difh, i'm fiwo riwosf.
    i fiiwj fnnvk!
    END

    Sample Output

    hello, i'm from mars.
    i like earth!

      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 }
     38 char* finds(trie root,char *str)
     39 {
     40     trie node=root;
     41     char *p=str;
     42     int id;
     43     while(*p)
     44     {
     45         id=*p-'a';
     46         node=node->next[id];
     47         ++p;
     48         if(node==NULL)
     49             return NULL;
     50     }
     51     if(node->exist){
     52         //printf("%p
    ",node->ss);
     53         return node->ss;
     54     }
     55     else
     56         return NULL;
     57 }
     58 int main()
     59 {
     60     trie root=create();
     61     char str1[3001],str2[11],str[3001],*p;
     62     while(scanf("%s",str1))
     63     {
     64         if(strcmp(str1,"START")==0)
     65             continue;
     66         else if(strcmp(str1,"END")!=0){
     67             scanf("%s",str2);
     68             insert_(root,str2,str1);
     69         }
     70         else break;
     71     }
     72     getchar();
     73         while(gets(str1))
     74         {
     75             int j=0;
     76             if(strcmp(str1,"START")==0)
     77                 continue;
     78             if(strcmp(str1,"END")==0)
     79                 break;
     80             for(int i=0;str1[i]!='';i++)
     81             {
     82                 if(str1[i]>='a'&&str1[i]<='z')
     83                     str[j++]=str1[i];
     84                 else
     85                 {
     86                     str[j]='';
     87                     p=finds(root,str);
     88                     if(p)
     89                     {
     90                         printf("%s",p);
     91                     }
     92                     else
     93                         printf("%s",str);
     94                     j=0;
     95                     printf("%c",str1[i]);
     96                 }
     97             }
     98             printf("
    ");
     99         }
    100     return 0;
    101 }
  • 相关阅读:
    apicloud 运费计算js+页面
    css让字体细长
    vue 请求完接口后执行方法
    js监听当前页面再次加载
    用apicloud+vue的VueLazyload实现缓存图片懒加载
    git merge和git rebase的区别(转)
    yuan先生博客链接
    django组件之contenttype(一)
    Django的orm中get和filter的不同
    python第三方库requests详解
  • 原文地址:https://www.cnblogs.com/PrayG/p/5899631.html
Copyright © 2011-2022 走看看