Guest User

Untitled

a guest
May 23rd, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.93 KB | None | 0 0
  1. C++:
  2. #include <set>
  3. #include <string>
  4. #include <iostream>
  5.  
  6. int main(int argc, char **argv)
  7. {
  8. // Declare and Initialize some variables
  9. std::string word;
  10. std::set<std::string> wordcount;
  11. // Read words and insert in rb-tree
  12. while (std::cin >> word) wordcount.insert(word);
  13. // Print the result
  14. std::cout << "Words: " << wordcount.size() << std::endl;
  15. return 0;
  16. }
  17. ______________________________________________________________________________
  18. C:
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22.  
  23. typedef struct node *nodeptr;
  24. typedef struct node {
  25. char *word;
  26. int count;
  27. nodeptr next;
  28. } node;
  29.  
  30. #define NHASH 29989
  31. #define MULT 31
  32. nodeptr bin[NHASH];
  33.  
  34. unsigned int hash(char *p)
  35. { unsigned int h = 0;
  36. for ( ; *p; p++)
  37. h = MULT * h + *p;
  38. return h % NHASH;
  39. }
  40.  
  41. #define NODEGROUP 1000
  42. int nodesleft = 0;
  43. nodeptr freenode;
  44.  
  45. nodeptr nmalloc()
  46. { if (nodesleft == 0) {
  47. freenode = malloc(NODEGROUP*sizeof(node));
  48. nodesleft = NODEGROUP;
  49. }
  50. nodesleft--;
  51. return freenode++;
  52. }
  53.  
  54. #define CHARGROUP 10000
  55. int charsleft = 0;
  56. char *freechar;
  57.  
  58. char *smalloc(int n)
  59. { if (charsleft < n) {
  60. freechar = malloc(n+CHARGROUP);
  61. charsleft = n+CHARGROUP;
  62. }
  63. charsleft -= n;
  64. freechar += n;
  65. return freechar - n;
  66. }
  67.  
  68. void incword(char *s)
  69. { nodeptr p;
  70. int h = hash(s);
  71. for (p = bin[h]; p != NULL; p = p->next)
  72. if (strcmp(s, p->word) == 0) {
  73. (p->count)++;
  74. return;
  75. }
  76. p = nmalloc();
  77. p->count = 1;
  78. p->word = smalloc(strlen(s)+1);
  79. strcpy(p->word, s);
  80. p->next = bin[h];
  81. bin[h] = p;
  82. }
  83.  
  84. int main()
  85. { int i;
  86. nodeptr p;
  87. char buf[16384];
  88. unsigned total = 0;
  89. for (i = 0; i < NHASH; i++)
  90. bin[i] = NULL;
  91. while (scanf("%s", buf) != EOF)
  92. incword(buf);
  93. for (i = 0; i < NHASH; i++)
  94. for (p = bin[i]; p != NULL; p = p->next)
  95. total += p->count ? 1 : 0;
  96. printf("Words: %d\n", total);
  97. return 0;
  98. }
Add Comment
Please, Sign In to add comment