Babelfish(字典树-map映射功能)

拼搏现实的明天。 2022-08-02 00:16 267阅读 0赞

Babelfish














Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 35663   Accepted: 15256

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.

字典树!
原来字典树也可以这么玩,原来可不知道!用一个二维数组来存这个字符串的映射值!真的很方便哦1

  1. #include<cstdio>
  2. #include<iostream>
  3. #include<cstring>
  4. using namespace std;
  5. char word[100010][15];
  6. struct Tree
  7. {
  8. int num;
  9. Tree *next[26];
  10. Tree()
  11. {
  12. num=0;
  13. for(int i=0;i<26;i++)
  14. {
  15. next[i]=NULL;
  16. }
  17. }
  18. }*root;
  19. void insert(Tree *p,char *s,int n)
  20. {
  21. int i=0;
  22. while(s[i])
  23. {
  24. int x=s[i]-'a';
  25. if(p->next[x]==NULL)
  26. {
  27. p->next[x]=new Tree();
  28. }
  29. p=p->next[x];
  30. i++;
  31. }
  32. p->num=n;
  33. }
  34. int find(Tree *p,char *s)
  35. {
  36. int i=0;
  37. while(s[i])
  38. {
  39. int n=s[i]-'a';
  40. if(p->next[n]==NULL)
  41. {
  42. return 0;
  43. }
  44. else
  45. {
  46. p=p->next[n];
  47. }
  48. i++;
  49. }
  50. return p->num;
  51. }
  52. int main()
  53. {
  54. int n,m,stl,i,j,f=1,ans;
  55. char s[15],c;
  56. char s1[15];
  57. root=new Tree();
  58. while(c=getchar())
  59. {
  60. if(c=='\n')
  61. {
  62. break;
  63. }
  64. else
  65. {
  66. ungetc(c,stdin);
  67. scanf("%s%s%*c",&s,&s1);
  68. insert(root,s1,f);
  69. strcpy(word[f],s);
  70. f++;
  71. }
  72. }
  73. while(gets(s))
  74. {
  75. ans=find(root,s);
  76. if(ans==0)
  77. {
  78. printf("eh\n");
  79. }
  80. else
  81. {
  82. printf("%s\n",word[ans]);
  83. }
  84. }
  85. }

发表评论

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

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

相关阅读

    相关 映射Map

    数学理解: ![在这里插入图片描述][watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9n