fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. typedef struct node
  5. {
  6. int isword;
  7. char word[12];
  8. struct node *next[26];
  9. node()
  10. {
  11. isword = 0;
  12. memset(next, NULL, sizeof next );
  13. }
  14. } *Tree, T;
  15. Tree root = new node();
  16.  
  17. void insert(char *wd, char *s)
  18. {
  19. int tmp;
  20. Tree p = root;
  21. while(*s)
  22. {
  23. tmp = *s - 'a';
  24. if(NULL == p->next[tmp])
  25. {
  26. p->next[tmp] = new node();
  27. }
  28. p = p->next[tmp];
  29. s++;
  30. }
  31. p->isword = 1;
  32. strcpy(p->word, wd);
  33. }
  34.  
  35. char *find(char *s)
  36. {
  37. int tmp;
  38. Tree p = root;
  39. while(*s)
  40. {
  41. int tmp = *s - 'a';
  42. if(NULL == p->next[tmp])
  43. return NULL;
  44. p = p->next[tmp];
  45. s++;
  46. }
  47. if(p->isword == 1)
  48. return p->word;
  49. else //这一句忘加了,怒送了7、8发!!!!
  50. return NULL;
  51. }
  52.  
  53. int main()
  54. {
  55. char word[12], a[12], b[12];
  56. char str[3100], *p;
  57. int i, j, k, len;
  58. //freopen("d:\\in.txt","r",stdin);
  59. scanf("%s",a);
  60. while(scanf("%s",a)&&strcmp(a,"END")!=0)
  61. {
  62. scanf("%s",b);
  63. insert(a,b);
  64. }
  65. scanf("%s",a);
  66. getchar();
  67. while(gets(str)&&strcmp(str,"END")!=0)
  68. {
  69. len = strlen(str);
  70. for(i=0; i<len; ++i)
  71. {
  72. if(str[i]>='a' && str[i]<='z')
  73. {
  74. j = i;
  75. while(j<len&&str[j]>='a'&&str[j]<='z') j++;
  76. k = 0;
  77. for(int p=i; p<j; ++p)
  78. {
  79. word[k++] = str[p];
  80. }
  81. i = j-1;
  82. word[k] ='\0';
  83. p = find(word);
  84. if(p)
  85. printf("%s",p);
  86. else
  87. printf("%s",word);
  88.  
  89. }
  90. else
  91. {
  92. printf("%c",str[i]);
  93. }
  94. }
  95. puts("");
  96. }
  97. return 0;
  98. }
  99.  
Time limit exceeded #stdin #stdout 5s 3472KB
stdin
Standard input is empty
stdout
Standard output is empty