Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- C++:
- #include <set>
- #include <string>
- #include <iostream>
- int main(int argc, char **argv)
- {
- // Declare and Initialize some variables
- std::string word;
- std::set<std::string> wordcount;
- // Read words and insert in rb-tree
- while (std::cin >> word) wordcount.insert(word);
- // Print the result
- std::cout << "Words: " << wordcount.size() << std::endl;
- return 0;
- }
- ______________________________________________________________________________
- C:
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- typedef struct node *nodeptr;
- typedef struct node {
- char *word;
- int count;
- nodeptr next;
- } node;
- #define NHASH 29989
- #define MULT 31
- nodeptr bin[NHASH];
- unsigned int hash(char *p)
- { unsigned int h = 0;
- for ( ; *p; p++)
- h = MULT * h + *p;
- return h % NHASH;
- }
- #define NODEGROUP 1000
- int nodesleft = 0;
- nodeptr freenode;
- nodeptr nmalloc()
- { if (nodesleft == 0) {
- freenode = malloc(NODEGROUP*sizeof(node));
- nodesleft = NODEGROUP;
- }
- nodesleft--;
- return freenode++;
- }
- #define CHARGROUP 10000
- int charsleft = 0;
- char *freechar;
- char *smalloc(int n)
- { if (charsleft < n) {
- freechar = malloc(n+CHARGROUP);
- charsleft = n+CHARGROUP;
- }
- charsleft -= n;
- freechar += n;
- return freechar - n;
- }
- void incword(char *s)
- { nodeptr p;
- int h = hash(s);
- for (p = bin[h]; p != NULL; p = p->next)
- if (strcmp(s, p->word) == 0) {
- (p->count)++;
- return;
- }
- p = nmalloc();
- p->count = 1;
- p->word = smalloc(strlen(s)+1);
- strcpy(p->word, s);
- p->next = bin[h];
- bin[h] = p;
- }
- int main()
- { int i;
- nodeptr p;
- char buf[16384];
- unsigned total = 0;
- for (i = 0; i < NHASH; i++)
- bin[i] = NULL;
- while (scanf("%s", buf) != EOF)
- incword(buf);
- for (i = 0; i < NHASH; i++)
- for (p = bin[i]; p != NULL; p = p->next)
- total += p->count ? 1 : 0;
- printf("Words: %d\n", total);
- return 0;
- }
Add Comment
Please, Sign In to add comment