hdu--1075--(字典树一般)

来源:互联网 发布:gis软件平台 编辑:程序博客网 时间:2024/06/05 04:57

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 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('\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.
 

Output
In 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;  }