void BytesTrieTest::TestTruncatingIteratorFromLinearMatchLong() { static const StringAndValue data[]={ { "abcdef", 10 }, { "abcdepq", 200 }, { "abcdeyz", 3000 } }; LocalPointer<BytesTrie> trie(buildTrie(data, UPRV_LENGTHOF(data), USTRINGTRIE_BUILD_FAST)); if(trie.isNull()) { return; // buildTrie() reported an error } // Go into a linear-match node. trie->next('a'); trie->next('b'); trie->next('c'); IcuTestErrorCode errorCode(*this, "TestTruncatingIteratorFromLinearMatchLong()"); // Truncate after the linear-match node. BytesTrie::Iterator iter(*trie, 3, errorCode); if(errorCode.logIfFailureAndReset("BytesTrie::Iterator(trie) constructor")) { return; } static const StringAndValue expected[]={ { "def", 10 }, { "dep", -1 }, { "dey", -1 } }; checkIterator(iter, expected, UPRV_LENGTHOF(expected)); // Reset, and we should get the same result. logln("after iter.reset()"); checkIterator(iter.reset(), expected, UPRV_LENGTHOF(expected)); }
// Takes FILE pointer of dictionary, filename for error messages and // pointer to root of Trie. // processes each line in the file, sending the code to buildTrie // returns the root after all word insertions struct node* getDict(FILE* fp, char* fileName, struct node* root) { char* line = (char*)malloc(sizeof(char) * MAXLINE); int* code = (int*)malloc(sizeof(int) * MAXLINE); char* word = (char*) malloc(sizeof(char) * MAXLINE); // check malloc if ((line == NULL) || (code == NULL) || (word == NULL)) fprintf(stderr, MLCFAIL); while ((fgets(line, MAXLINE, fp)) != NULL) { int i = 0; while ((line[i] != '\0') && (line[i] != '\n') && (i < MAXLINE)) { // convert to T9code code[i] = getCode(tolower(line[i])); i++; } // trim to word strncpy(word, line, i); root = buildTrie(code, word, i, 0, root); } free(word); free(line); free(code); return root; }
void BytesTrieTest::checkData(const StringAndValue data[], int32_t dataLength, UStringTrieBuildOption buildOption) { LocalPointer<BytesTrie> trie(buildTrie(data, dataLength, buildOption)); if(trie.isNull()) { return; // buildTrie() reported an error } checkFirst(*trie, data, dataLength); checkNext(*trie, data, dataLength); checkNextWithState(*trie, data, dataLength); checkNextString(*trie, data, dataLength); checkIterator(*trie, data, dataLength); }
Trie_node* createTree(const char *dict_path, const char* Lextrees_path) { Trie_node *root = new Trie_node; root->son_num = 0; for (int i = 0; i < max_son_num; ++i) root->next[i] = NULL; root->is_str = false; buildTrie(dict_path, root); //printf("%d\n",node_num); getLexPara(root, -1, -1, -1); writeLextrees(Lextrees_path); return root; }
int main() { root = new Node(); ans = 0; char* dial; dial = (char*)malloc(sizeof(char) * 100); scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%s", dial); buildTrie(dial); } printf("%d\n", ans); return 0; }
vector<string> findWords(vector<vector<char>>& board, vector<string>& words) { auto root = buildTrie(words); vector<string> solution; for (size_t i = 0; i < board.size(); ++i) { for (size_t j = 0; j < board[i].size(); ++j) { unordered_set<size_t> visited; string currentWord; search_trie(root.get(), i, j, board, visited, solution, currentWord); } } return solution; }
std::vector<std::string> findWords( std::vector<std::vector<char>>& B, std::vector<std::string>& W) { if (B.empty() || B[0].empty()) return m_rslt; m_B = B; m_h = B.size(); m_w = B[0].size(); TrieNode* root = buildTrie(W); for (int y = 0; y < m_h; ++y) { for (int x = 0; x < m_w; ++x) { dfs(y, x, root, 0); } } return m_rslt; }
void UCharsTrieTest::TestNextForCodePoint() { static const StringAndValue data[]={ { "\\u4dff\\U00010000\\u9999\\U00020000\\udfff\\U0010ffff", 2000000000 }, { "\\u4dff\\U00010000\\u9999\\U00020002", 44444 }, { "\\u4dff\\U000103ff", 99999 } }; LocalPointer<UCharsTrie> trie(buildTrie(data, UPRV_LENGTHOF(data), USTRINGTRIE_BUILD_FAST)); if(trie.isNull()) { return; // buildTrie() reported an error } UStringTrieResult result; if( (result=trie->nextForCodePoint(0x4dff))!=USTRINGTRIE_NO_VALUE || result!=trie->current() || (result=trie->nextForCodePoint(0x10000))!=USTRINGTRIE_NO_VALUE || result!=trie->current() || (result=trie->nextForCodePoint(0x9999))!=USTRINGTRIE_NO_VALUE || result!=trie->current() || (result=trie->nextForCodePoint(0x20000))!=USTRINGTRIE_NO_VALUE || result!=trie->current() || (result=trie->nextForCodePoint(0xdfff))!=USTRINGTRIE_NO_VALUE || result!=trie->current() || (result=trie->nextForCodePoint(0x10ffff))!=USTRINGTRIE_FINAL_VALUE || result!=trie->current() || trie->getValue()!=2000000000 ) { errln("UCharsTrie.nextForCodePoint() fails for %s", data[0].s); } if( (result=trie->firstForCodePoint(0x4dff))!=USTRINGTRIE_NO_VALUE || result!=trie->current() || (result=trie->nextForCodePoint(0x10000))!=USTRINGTRIE_NO_VALUE || result!=trie->current() || (result=trie->nextForCodePoint(0x9999))!=USTRINGTRIE_NO_VALUE || result!=trie->current() || (result=trie->nextForCodePoint(0x20002))!=USTRINGTRIE_FINAL_VALUE || result!=trie->current() || trie->getValue()!=44444 ) { errln("UCharsTrie.nextForCodePoint() fails for %s", data[1].s); } if( (result=trie->reset().nextForCodePoint(0x4dff))!=USTRINGTRIE_NO_VALUE || result!=trie->current() || (result=trie->nextForCodePoint(0x10000))!=USTRINGTRIE_NO_VALUE || result!=trie->current() || (result=trie->nextForCodePoint(0x9999))!=USTRINGTRIE_NO_VALUE || result!=trie->current() || (result=trie->nextForCodePoint(0x20222))!=USTRINGTRIE_NO_MATCH || result!=trie->current() // no match for trail surrogate ) { errln("UCharsTrie.nextForCodePoint() fails for \\u4dff\\U00010000\\u9999\\U00020222"); } if( (result=trie->reset().nextForCodePoint(0x4dff))!=USTRINGTRIE_NO_VALUE || result!=trie->current() || (result=trie->nextForCodePoint(0x103ff))!=USTRINGTRIE_FINAL_VALUE || result!=trie->current() || trie->getValue()!=99999 ) { errln("UCharsTrie.nextForCodePoint() fails for %s", data[2].s); } }
vector<string> findWords(vector<vector<char>>& board, vector<string>& words) { vector<string> res; if (words.empty() || board.empty()) return res; int row = board.size(); int col = board[0].size(); int wsize = words.size(); Trie *root = new Trie(); for (int i = 0; i < wsize; ++i) { buildTrie(root, words[i], i); } //buildTrie(root, ) for (int i = 0; i < row; ++i) { for (int j = 0; j < col; ++j) { findWord(res, board, root, row, col, i, j, words); } } return res; }
BytesTrie *BytesTrieTest::buildMonthsTrie(UStringTrieBuildOption buildOption) { // All types of nodes leading to the same value, // for code coverage of recursive functions. // In particular, we need a lot of branches on some single level // to exercise a split-branch node. static const StringAndValue data[]={ { "august", 8 }, { "jan", 1 }, { "jan.", 1 }, { "jana", 1 }, { "janbb", 1 }, { "janc", 1 }, { "janddd", 1 }, { "janee", 1 }, { "janef", 1 }, { "janf", 1 }, { "jangg", 1 }, { "janh", 1 }, { "janiiii", 1 }, { "janj", 1 }, { "jankk", 1 }, { "jankl", 1 }, { "jankmm", 1 }, { "janl", 1 }, { "janm", 1 }, { "jannnnnnnnnnnnnnnnnnnnnnnnnnnnn", 1 }, { "jano", 1 }, { "janpp", 1 }, { "janqqq", 1 }, { "janr", 1 }, { "januar", 1 }, { "january", 1 }, { "july", 7 }, { "jun", 6 }, { "jun.", 6 }, { "june", 6 } }; return buildTrie(data, UPRV_LENGTHOF(data), buildOption); }
vector<string> Solution::findWords(vector<vector<char> > &board, vector<string> &words) { vector<string> res; int row = board.size(); if(0==row) return res; int col = board[0].size(); if(0==col) return res; int wordCount = words.size(); if(0==wordCount) return res; Trie *root = buildTrie(words); int i,j; for(i =0 ; i<row; i++) { for(j=0; j<col && wordCount > res.size(); j++) { checkWords(board, i, j, row, col, root, res, words); } } return res; }
/** * Checks the spelling of a list of words against a list of dictionary words. * Any misspellings are reported to a callback function, along with a suggested * correction. The suggested correction is chosen to minimise the "edit * difference" between it and the original word. If no dictionary word is * within the maximum allowable distance from the original word, the callback * receives NULL instead of a suggestion. * * The callback function returns either TRUE or FALSE, indicating whether the * suggested correction should be applied. If TRUE, the memory allocated to the * original word is realloc'd to make space for the corrected word, which is * then copied into it. * * Parameters: * text - an array of words to spell check (each word must be * dynamically allocated); * textLength - the number of words to spell check; * dict - an array of words to use as the dictionary; * dictLength - the number of dictionary words; * maxDifference - the maximum difference between misspelt words and their * suggested corrections; * action' - a pointer to a function that will be called for each * misspelt word. */ void check(char* text[], int textLength, char* dict[], int dictLength, int maxDifference, ActionFunc action) { /* Construct a trie to represent the set of all the dictionary words. */ TrieNode* trie = buildTrie(dict, dictLength); char* suggestion = NULL; int maxLen = 0; int t; for(t = 0; t < textLength; t++) { int len = strlen(text[t]); if(maxLen < len) { /* Maintain a buffer to store, temporarily, the correction for each * word. Increase the size of the buffer as needed. */ maxLen = len; suggestion = (char*)realloc(suggestion, sizeof(char) * (maxDifference + maxLen + 1)); } /* Check whether the word, or an edited version thereof, is in the trie. */ if(!inTrie(trie, text[t], suggestion, maxDifference)) { /* Misspelling, with no suggestion available. */ (*action)(text[t], NULL); } else if(strcmp(text[t], suggestion) != 0) { /* Misspelling, with suggested correction */ if((*action)(text[t], suggestion)) { text[t] = (char*)realloc(text[t], (strlen(suggestion) + 1) * sizeof(char)); strcpy(text[t], suggestion); } } } free(suggestion); freeTrie(trie); }