POJ 2503 Babelfish

曾经终败给现在 2022-09-29 12:55 200阅读 0赞

Babelfish














Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 42986   Accepted: 18199

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

  1. dog ogday
  2. cat atcay
  3. pig igpay
  4. froot ootfray
  5. loops oopslay
  6. atcay
  7. ittenkay
  8. oopslay

Sample Output

  1. cat
  2. eh
  3. loops

Hint

Huge input and output,scanf and printf are recommended.

Source

Waterloo local 2001.09.22

//字典树:

  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cstring>
  4. #include <map>
  5. #include <cstdio>
  6. #include <algorithm>
  7. using namespace std;
  8. struct Trie
  9. {
  10. int cnt;
  11. bool flag;
  12. char s[45];//保存的字符串
  13. struct Trie *next[26];
  14. Trie():cnt(0),flag(false){memset(next,0,sizeof(next));}
  15. };
  16. Trie *root = NULL;
  17. void Trie_insert(char *str,char *tmp)
  18. {
  19. Trie *p = root;
  20. for(int i=0;i<strlen(str);i++)
  21. {
  22. if(p -> next[str[i] - 'a' ]== NULL)
  23. {
  24. p -> next[str[i] - 'a'] = new Trie;
  25. }
  26. p = p ->next[str[i] - 'a'];
  27. }
  28. p->cnt++;//走到字符串的最后一个字母的节点
  29. strcpy(p->s,tmp);
  30. p->flag=1;
  31. }
  32. void Trie_search(char *str)
  33. {
  34. Trie *p = root;
  35. for(int i=0;i<strlen(str);i++)
  36. {
  37. if(p -> next[str[i] - 'a'] == NULL)
  38. {
  39. cout<<"eh"<<endl;
  40. return;
  41. }
  42. else p = p -> next[str[i] - 'a'];
  43. }
  44. cout<<p ->s<<endl;
  45. }
  46. int main()
  47. {
  48. char str[30],s1[15],s2[15];
  49. root = new Trie;
  50. while(gets(str))
  51. {
  52. if(str[0] == '\0')
  53. break;
  54. sscanf(str,"%s %s",s1,s2);
  55. Trie_insert(s2,s1);
  56. }
  57. while(scanf("%s",str)!=EOF)
  58. Trie_search(str);
  59. return 0;
  60. }

//map:

  1. #include <map>
  2. #include <cstdio>
  3. #include <algorithm>
  4. using namespace std;
  5. int main()
  6. {
  7. char str[30],s1[15],s2[15];
  8. root = new Trie;
  9. while(gets(str))
  10. {
  11. if(str[0] == '\0')
  12. break;
  13. sscanf(str,"%s %s",s1,s2);
  14. Trie_insert(s2,s1);
  15. }
  16. while(scanf("%s",str)!=EOF)
  17. Trie_search(str);
  18. return 0;
  19. }
  20. int main()
  21. {
  22. map<string,string> m;
  23. string s1,s2,s;
  24. while(getline(cin,s))
  25. {
  26. if(s == "")
  27. break;
  28. s1 = "";
  29. s2 = "";
  30. int sum = 0;
  31. for(int i=0;i<s.size();i++)
  32. {
  33. if(s[i] == ' ')
  34. break;
  35. s1 += s[i];
  36. ++sum;
  37. }
  38. sum +=1;
  39. for(;sum<s.size();sum++)
  40. {
  41. s2 += s[sum];
  42. }
  43. m[s2] = s1;
  44. }
  45. while(cin>>s)
  46. {
  47. if(m[s] != "")
  48. cout<<m[s]<<endl;
  49. else cout<<"eh"<<endl;
  50. }
  51. m.clear();
  52. return 0;
  53. }

发表评论

表情:
评论列表 (有 0 条评论,200人围观)

还没有评论,来说两句吧...

相关阅读

    相关 POJ 2503——Babelfish

    这一题也没什么思路很直接,直接用字典树存储信息,在进行查找: 优点:思路直接,速度较快      缺点:占用内存较大,建立的关系是单向的,代码较长 include