-
-
Save anonymous/3f55b47ca062b60c8091 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#define ARRAY_SIZE(a) sizeof(a)/sizeof(a[0]) | |
#define ALPHABET_SIZE (26) | |
// Converts key current character into index | |
// use only 'a' through 'z' and lower case | |
#define CHAR_TO_INDEX(C) ((int)C - (int)'A') | |
#define INDEX_TO_CHAR(IX) ('A' + IX) | |
char words[3000][40]={{0}}; | |
int counters[3000]={0}; | |
int wordnr=0; | |
typedef struct trie_node trie_node_t; | |
struct trie_node | |
{ | |
int counter; | |
trie_node_t *children[ALPHABET_SIZE]; | |
}; | |
typedef struct trie trie_t; | |
struct trie | |
{ | |
trie_node_t *root; | |
int count; | |
}; | |
// Returns new trie node | |
trie_node_t *getNode(void) | |
{ | |
trie_node_t *pNode = NULL; | |
pNode = (trie_node_t *)malloc(sizeof(trie_node_t)); | |
if( pNode ) | |
{ | |
int i; | |
pNode->counter = 0; | |
for(i = 0; i < ALPHABET_SIZE; i++) | |
{ | |
pNode->children[i] = NULL; | |
} | |
} | |
return pNode; | |
} | |
// Initializes trie | |
void initialize(trie_t *pTrie) | |
{ | |
pTrie->root = getNode(); | |
pTrie->count = 0; | |
} | |
void setorder_rec(trie_node_t *pCrawl, char *str, int n) | |
{ | |
if (pCrawl == NULL) return; | |
if (pCrawl->counter) { | |
str[n]='\0'; | |
strcpy(words[wordnr],str); | |
words[wordnr][strlen(str)]='\0'; | |
counters[wordnr]=pCrawl->counter; | |
wordnr++; | |
printf("%.*s: %d\n", n, str, pCrawl->counter); | |
} | |
for (int i = 0; i < ALPHABET_SIZE; i++) { | |
str[n] = INDEX_TO_CHAR(i); | |
setorder_rec(pCrawl->children[i], str, n + 1); | |
} | |
} | |
void setorder(trie_t *pTrie) | |
{ | |
char tempword[40] = {0}; | |
setorder_rec(pTrie->root, tempword, 0); | |
} | |
void insert(trie_t *pTrie, char key[]) | |
{ | |
int level; | |
int length = strlen(key); | |
int index; | |
trie_node_t *pCrawl; | |
pTrie->count++; | |
pCrawl = pTrie->root; | |
for( level = 0; level < length; level++ ) | |
{ | |
index = CHAR_TO_INDEX(key[level]); | |
if( !pCrawl->children[index] ) | |
{ | |
pCrawl->children[index] = getNode(); | |
} | |
pCrawl = pCrawl->children[index]; | |
} | |
pCrawl->counter++; | |
} | |
void bubble_sort() | |
{ | |
int i,j,temp; | |
int n=15; | |
char tempwordy[40]={0}; | |
for(i=1;i< n;i++) | |
{ | |
for(j=0;j< n-1;j++) | |
{ | |
if(counters[j]>counters[j+1]) | |
{ | |
printf("swapping counters[%d]=%d with counters[%d]=%d\n", j, counters[j], j+1, counters[j+1]); | |
temp=counters[j]; | |
counters[j]=counters[j+1]; | |
counters[j+1]=temp; | |
strcpy(tempwordy,words[j]); | |
strcpy(words[j],words[j+1]); | |
strcpy(words[j+1],tempwordy); | |
} | |
} | |
} | |
} | |
int main(int argc, char *argv[]) | |
{ | |
char *keys[argc]; | |
for(int i=0;i<argc;i++) | |
keys[i] = (char *) malloc(strlen(argv[i+1]+1)); | |
for(int i=0;i<argc;i++) | |
strcpy(keys[i], argv[i+1]); | |
if (argc < 2) { | |
fprintf(stderr, "Must pass an argument!\n"); | |
exit(1); | |
} | |
trie_t trie; | |
initialize(&trie); | |
// Construct trie | |
for(int i = 0; i < ARRAY_SIZE(keys); i++) | |
{ | |
insert(&trie, keys[i]); | |
} | |
setorder(&trie); | |
bubble_sort(); | |
for(int i=argc-1; i>=0; i--) | |
{ | |
printf("#%d %s=%d\n", i, words[i], counters[i]); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment