[ create a new paste ] login | about

Link: http://codepad.org/J6oFQJMb    [ raw code | output | fork ]

C, pasted on Oct 31:
   struct trie {
        struct trie *child[26];
        int count;
        char letter;
    };
struct trie* newtrie(char newchar) {
    struct trie* r = malloc(sizeof(struct trie));
    memset(r, 0, sizeof(struct trie));
    r->letter = newchar;
    return r;
}

int addWordOccurrence(const char* word)
{

    struct trie *root = newtrie(0);
    struct trie *initRoot=root;
    int count;

    int x=strlen(word);
    printf("%d",x);
    int i;
    for(i=0; i<x; i++)
    {  
        int z=word[i]-97;
        if(word[i]=='\n')
        {
            z=word[i-1]-97;
            root->child[z]->count++;
            root=initRoot;
        }

    if (root->child[z] == NULL)
        root->child[z] = newtrie(word[i]);
    root->child[z]=root;
    }
    return 0;
}

int main() {
    addWordOccurrence("cat\ndog\napple\n");
    addWordOccurrence("cat\ndog\napple\n");
    addWordOccurrence("cat\ndog\napple\n");
    addWordOccurrence("cat\ndog\napple\n");
    addWordOccurrence("cat\ndog\napple\n");
    addWordOccurrence("cat\ndog\napple\n");
    return 0;
}


Output:
1
141414141414


Create a new paste based on this one


Comments: