hdu–1075–(字典樹一般)

來源:互聯網
上載者:User
What Are You Talking About

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/204800 K (Java/Others)
Total Submission(s): 10424    Accepted Submission(s): 3320


Problem DescriptionIgnatius 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? 


InputThe 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('\t'), enter('\n') 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. 


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


Sample Input

STARTfrom fiwohello difhmars riwosfearth fnnvklike fiiwjENDSTARTdifh, i'm fiwo riwosf.i fiiwj fnnvk!END
 


Sample Output

hello, i'm from mars.i like earth!    
#include<iostream>   #include<cstdio>   #include<cstring>   #include<string>   #include<algorithm>   using namespace std;  #define N 3010     struct Tire //字典樹,dic存英文含義   {      char dic[11];      Tire *next[26];  };  Tire *head;    void init() //初始化   {      head = new Tire;      head->dic[0] = '*';      for(int i = 0; i < 26; ++i)          head->next[i] = NULL;  }    void insert(char eng[], char mar[]) //插入英文,火星字典   {      int temp, len;      Tire *cur;      len = strlen(mar);      cur = head;      for(int i = 0; i < len; ++i)      {          temp = mar[i] - 'a';          if(cur->next[temp] == NULL)          {              cur->next[temp] = new Tire;              cur = cur->next[temp];              cur->dic[0] = '*';              for(int j = 0; j < 26; ++j)                  cur->next[j] = NULL;          }          else              cur = cur->next[temp];      }      strcpy(cur->dic, eng); //儲存英文含義   }    void del(Tire *head) //動態建樹,用完釋放記憶體   {      for(int i = 0; i < 26; ++i)          if(head->next[i] != NULL)              del(head->next[i]);      delete(head);  }    string search(string mar) //尋找火星詞典   {      int len, temp;      Tire *cur;      len = mar.length();      cur = head;      for(int i = 0; i < len; ++i)      {          temp = mar[i] - 'a';          if(cur->next[temp] == NULL)              return mar;          cur = cur->next[temp];      }      if(cur->dic[0] == '*')          return mar;      return cur->dic;  }    int main()  {      init();      char dic[10];      char eng[11], mar[11]; //英文,火星文       char history[N]; //火星文章       string word;      string answer; //文章結果       answer = "";      word = "";      scanf("%s", dic);      while(scanf("%s", eng) && strcmp(eng, "END") != 0) //插入火星字典       {             scanf("%s", mar);          insert(eng, mar);      }      scanf("%s", dic);      getchar();      while(gets(history) && strcmp(history, "END") != 0) //火星文章       {          int len;          len = strlen(history);          answer = ""; //翻譯結果           for(int i = 0; i < len; ++i)          {                if(history[i] >= 'a' && history[i] <= 'z') //取出每個單詞                   word += history[i];              else              {                  answer += search(word); //翻譯單詞                   word = ""; //清空                   answer += history[i]; //加入標點               }          }          cout<<answer<<endl;      }      del(head); //釋放記憶體       return 0;  }  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.