POJ 2503-Babelfish【字典树】

逃离我推掉我的手 2022-07-24 07:06 214阅读 0赞

Babelfish














Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 39270   Accepted: 16778

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<stdio.h>
  2. #include<string.h>
  3. #include<algorithm>
  4. using namespace std;
  5. char word[100100][12];
  6. typedef struct node
  7. {
  8. int cnt;
  9. node *next[26];
  10. }Trie;
  11. Trie root;
  12. void ct(char *str,int jj)
  13. {
  14. //printf("%d\n",jj);
  15. int len,i,j;
  16. len=strlen(str);
  17. Trie *p=&root,*q;
  18. for(i=0;i<len;i++)
  19. {
  20. int id=str[i]-'a';
  21. if(p->next[id]==NULL)
  22. {
  23. q=new Trie;
  24. if(i==len-1)
  25. {
  26. q->cnt=jj;
  27. }
  28. for(j=0;j<26;j++)
  29. q->next[j]=NULL;
  30. p->next[id]=q;
  31. p=p->next[id];
  32. }
  33. else
  34. {
  35. if(i==len-1)
  36. {
  37. p->next[id]->cnt=jj;
  38. }
  39. p=p->next[id];
  40. }
  41. }
  42. }
  43. int find(char *str)
  44. {
  45. int len,i,j,id;
  46. len=strlen(str);
  47. Trie *p=&root;
  48. for(i=0;i<len;i++)
  49. {
  50. id=str[i]-'a';
  51. if(p->next[id]==NULL)
  52. return -1;
  53. //printf("%d\n",p->next[id]->cnt);
  54. if(i==len-1)
  55. {
  56. return p->next[id]->cnt;
  57. }
  58. p=p->next[id];
  59. }
  60. return -1;
  61. }
  62. int main()
  63. {
  64. char hh[15];
  65. char map[15];
  66. int io=0;
  67. while(true)
  68. {
  69. char t;
  70. if((t=getchar())=='\n')
  71. break;
  72. else
  73. {
  74. map[0]=t;
  75. int i=1;
  76. while(true)
  77. {
  78. t=getchar();
  79. if(t==' ')
  80. {
  81. map[i]='\0';
  82. break;
  83. }
  84. else
  85. map[i++]=t;
  86. }
  87. }
  88. strcpy(word[io++],map);
  89. scanf("%s",hh);
  90. ct(hh,io-1);
  91. getchar();
  92. }
  93. while(scanf("%s",hh)!=EOF)
  94. {
  95. int xx=find(hh);
  96. //printf("%d\n",xx);
  97. if(xx==-1)
  98. {
  99. printf("eh\n");
  100. }
  101. else
  102. {
  103. printf("%s\n",word[xx]);
  104. }
  105. }
  106. return 0;
  107. }

发表评论

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

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

相关阅读

    相关 POJ 2503——Babelfish

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