int main() { int rc, count_case, size_book; Node *trie; rc = scanf("%d", &count_case); /* Dummy check to pass the CI. */ if (rc == EOF) { return 1; } while (count_case) { rc = scanf("%d", &size_book); /* Dummy check to pass the CI. */ if (rc == EOF) { return 1; } init_trie(&trie); construct_trie(trie, size_book); deinit_trie(trie); count_case--; } return 0; }
int main(int argc, char** argv) { const char* word = "bananas"; trie_t trie; init_trie(&trie); insert_trie(trie, word); }
void init_hyph () { if (trie_not_ready) init_trie(); l_hyf = lhmin; r_hyf = rhmin; cur_lang = 0; }
int main() { int i; init_trie(); init(); search(); output(); return 0; }
int main() { int i; init_trie(); init(); search(); for(i = 1;i <= w; i++) printf("%d %d %c\n", ans[i].r, ans[i].c, ans[i].d + 'A'); return 0; }
// trie construction with a depth-first traverse (dfs) Trie* build_trie(Rule *rules, int nrules, int leaf_rules) { Trie* v; int i; total_rules = nrules; LEAF_RULES = leaf_rules; root_node = init_trie(rules, nrules); create_children(root_node); dump_stats(); }
void construct_trie(Node *trie, int size_book) { int rc, i, idx_phone, len_phone, number; Node *curr, *succ; char buf[BUF_SIZE + 1]; for (idx_phone = 0 ; idx_phone < size_book ; idx_phone++) { memset(buf, 0, sizeof(char) * (BUF_SIZE + 1)); rc = scanf("%s", buf); /* Dummy check to pass the CI. */ if (rc == EOF) { return; } len_phone = strlen(buf); curr = trie; for (i = 0 ; i < len_phone ; i++) { number = buf[i] - ASCII_BIAS; if (curr->succ[number] == NULL) { break; } curr = curr->succ[number]; /* Some of the previously recorded string is the prefix of the current string. */ if (curr->is_leaf == true) { printf("NO\n"); return; } } /* The current string is the prefix of some previously recorded string. */ if ((i == len_phone) && (curr->is_leaf == false)) { printf("NO\n"); return; } for (; i < len_phone ; i++) { number = buf[i] - ASCII_BIAS; init_trie(&succ); curr->succ[number] = succ; curr->is_leaf = false; curr = curr->succ[number]; } } printf("YES\n"); return; }
result write_messages(FILE *out, Messages *m) { int i; result return_code; init_trie(); for (i = 0; i < m->count; i++) { if(write_message(out, &m->messages[i]) == ERROR) { fprintf(stderr, "Error writing message %d\n", i); return_code = ERROR; goto free_trie; } } return_code = OK; free_trie: free_trie(); return return_code; }
/** Initializes a new, empty list, with the necessary structures to control concurrent thread access. @param free_function Each word can be associated to a generic pointer hereby denoted `data`. This function will be called to free a node's `data` when it is removed from the list. It can be NULL if nothing shall be done when deleting a node. See `destroy_word_list()` and `list_delete()` for further info. @param is_valid Pointer to function that returns `1` if a char is part of this wordlist's alphabet; `0` otherwise. @param pos_to_char Pointer to function that converts an index position from `edges` back to its character representation. @param char_to_pos Pointer to function that converts a character `s` into a valid, unique position. @param charcount How many characters compose this list's alphabet. @return A pointer to a new, empty word list instance, or `NULL` is there weren't enough resources to create a new list. */ Word_list_ptr init_word_list(void (*free_function)(void *), int (*is_valid)(char), char (*pos_to_char)( int), int (*char_to_pos)(char), int charcount) { Word_list_ptr new_list; new_list = malloc(sizeof(struct yaircd_list)); if (new_list == NULL) { return NULL; } if (pthread_mutex_init(&new_list->mutex, NULL) != 0) { free(new_list); return NULL; } new_list->free_func = free_function; if ((new_list->trie = init_trie(free_yaircd_node, is_valid, pos_to_char, char_to_pos, charcount)) == NULL) { pthread_mutex_destroy(&new_list->mutex); free(new_list); return NULL; } return new_list; }
int init() { int i, j, t, k; char sub[MAXM]; scanf("%d %d %d %d %d", &n, &m, &t, &p, &q); if(n == 0 && m == 0 && t == 0 && p == 0 && q == 0) return 0; getchar(); for(i = 1;i <= n; i++) { for(j = 1;j <= m; j++) { map[i][j] = getchar(); if(map[i][j] == '*') map[i][j] = '1'; } getchar(); } init_trie(); while(t--) { k = 0; getchar(); for(i = 1;i <= p; i++) { for(j = 1;j <= q; j++) { sub[k] = getchar(); if(sub[k] == '*') sub[k] = '1'; k++; } getchar(); } sub[k] = '\0'; insert(sub); } return 1; }
int find_max_xor_two_numbers_in_array_v2 (int* array, int len) { int index, bit_index, max_xor, num_max_xor, link; struct trienode* root = NULL; struct trienode* current; if (!array || (len <= 1)) { return(0); } init_trie(&root); if (!root) { return(0); } for (index = 0; index < len; ++index) { insert_into_trie(root, array[index]); } max_xor = 0; for (index = 0; index < len; ++index) { current = root; num_max_xor = array[index]; for (bit_index = sizeof(int) * CHAR_BIT - 1; bit_index >= 0; --bit_index) { if (array[index] & (1 << bit_index)) { link = 1; } else { link = 0; } if (link == 0) { if (!current->links[1]) { num_max_xor &= ~(1 << bit_index); current = current->links[0]; } else { num_max_xor |= (1 << bit_index); current = current->links[1]; } } else { if (!current->links[0]) { num_max_xor &= ~(1 << bit_index); current = current->links[1]; } else { num_max_xor |= (1 << bit_index); current = current->links[0]; } } } if (num_max_xor > max_xor) { max_xor = num_max_xor; } } free_trie(root); return(max_xor); }
int main(int argc, char *argv[]) { if (argc != 2) { fprintf(stderr, "%s\n", "Usage: t9 [FILE]"); return 1; } FILE *file = fopen(argv[1], "r"); if (!file) { fprintf(stderr, "Error: file not found\n"); return 1; } // insert dictionary into trie Tnode *trie = init_trie(); char line[MAX_LINE_LENGTH]; while (fgets(line, MAX_LINE_LENGTH, file)) { insert_word(trie, line); } fclose(file); // current position in trie Tnode *currentNode = trie; char input[MAX_LINE_LENGTH]; printf("Enter \"exit\" to quit.\n"); printf("Enter Key Sequence (or \"#\" for next word):\n"); printf("> "); while (fgets(input, MAX_LINE_LENGTH, stdin)) { // remove newline char *trim; trim = strchr(input, '\n'); if (trim) { *trim = '\0'; } if (strcmp(input, "exit") == 0 || feof(stdin)) { break; } // check for #, else lookup word if (strcmp(input, "#") == 0) { if (currentNode && currentNode != trie) { printf(" %s\n", currentNode->word); currentNode = currentNode->nodes[8]; } else { printf(" %s\n", "There are no more T9onyms"); } } else { currentNode = lookup_word(trie, input); if (!currentNode || !currentNode->word) { printf(" %s\n", "Not found in current dictionary."); } else { printf(" \'%s\'\n", currentNode->word); currentNode = currentNode->nodes[8]; } } printf("Enter Key Sequence (or \"#\" for next word):\n"); printf("> "); } destroy_trie(trie); return 0; }