void WordSearchII::findWords(const vector<vector<char>> &board, vector<vector<bool>> &visited, int row, int col, string word, set<string> &result) { int nrows = board.size(); int ncols = board[0].size(); if (row < 0 || row >= nrows || col < 0 || col >= ncols) return; if (visited[row][col]) return; word.push_back(board[row][col]); if (!trie.startsWith(word)) return; if (trie.search(word)) result.insert(word); visited[row][col] = true; findWords(board, visited, row + 1, col, word, result); findWords(board, visited, row - 1, col, word, result); findWords(board, visited, row, col + 1, word, result); findWords(board, visited, row, col - 1, word, result); visited[row][col] = false; }
//DFS bool findWords(vector<vector<char>> &board,int i,int j,const string& word,int index){ if(i<0||i>=board.size()||j<0||j>=board[0].size()||index>=word.size()||word[index]!=board[i][j]){ return false; } if(word[index]==board[i][j]&&index==word.size()-1){ return true; } char temp=board[i][j]; board[i][j]='-'; bool flag= findWords(board,i-1,j,word,index+1)||findWords(board,i+1,j,word,index+1)||findWords(board,i,j-1,word,index+1)||findWords(board,i,j+1,word,index+1); board[i][j]=temp; return flag; }
vector<string> findWords(vector<vector<char>>& board, vector<string>& words) { set<string>resultSet; sort(words.begin(),words.end()); for(int k=0;k<words.size();k++){ if(k-1>=0&&words[k]==words[k-1]){ continue; } for(int i=0;i<board.size();i++){ for(int j=0;j<board[i].size();j++){ if(words[k][0]==board[i][j]){ bool flag=findWords(board,i,j,words[k],0); if(flag){ resultSet.insert(words[k]); } } } } } vector<string>result(resultSet.begin(),resultSet.end()); return result; }
vector<string> WordSearchII::findWords(vector<vector<char>> &board, vector<string> &words) { set<string> result; vector<string> ret; int nrows = board.size(); if (nrows == 0) return ret; int ncols = board[0].size(); if (ncols == 0) return ret; if (words.size() == 0) return ret; for (auto &w : words) trie.insert(w); vector<vector<bool>> visited(nrows, vector<bool>(ncols, false)); for (int i = 0; i < nrows; i++) for (int j = 0; j < ncols; j++) findWords(board, visited, i, j, "", result); for (auto &w : result) ret.push_back(w); return ret; }
void findWords(vector<vector<char>>& board, int x, int y, TrieNode* root, string word){ if(x<0||x>=board.size() || y<0||y>=board[0].size() || board[x][y]==' ') return; int index = board[x][y]-'a'; if(root->sons[index]){ char ch = board[x][y]; word += board[x][y]; board[x][y] = ' '; root=root->sons[index]; if(root->isEnd) resSet.insert(word); findWords(board, x+1, y, root, word); findWords(board, x-1, y, root, word); findWords(board, x, y-1, root, word); findWords(board, x, y+1, root, word); board[x][y] = ch; } }
int solve(FILE *f) { SymbolTable *table = NULL; int i; table = createSymbolTable(makeDictionaryEntry, compareDictionaryEntry); getTableData(f, table); findWords(table); dropSymbolTable(table); return 0; }
vector<string> findWords(vector<vector<char>>& board, vector<string>& words) { vector<string> res; TrieNode node; for(int i = 0; i < words.size(); i++) node.Add(words[i], 0); string sol = ""; for(int i = 0; i < board.size(); i++) for(int j = 0; j< board[0].size(); j++) findWords(board, &node, i, j, sol, res); return res; }
vector<string> findWords(vector<vector<char>>& board, vector<string>& words) { Trie* trie = new Trie(words); TrieNode* root = trie->getRoot(); for(int i=0; i<board.size(); i++) for(int j=0; j<board[i].size(); j++) findWords(board, i, j, root, ""); vector<string> res; for(auto it:resSet) res.push_back(it); return res; }
InputMatcherWordErrorList InputMatcher::findWords(const QDawg::Node* node, int set, int maxset, const QString& str, uint error, bool allowprefix, bool predict) const { if ( !node || (set >= maxset && !predict) ) return InputMatcherWordErrorList(); InputMatcherWordErrorList r; const InputMatcherGuessList *g = 0; if ( set < maxset ) { g = nsets[set]; // no letters to follow, don't try and make word, invalid path. if (g->count() == 0) { return InputMatcherWordErrorList(); } } while (node) { QChar ch = node->letter(); IMIGuess guess; if (g && g->contains(ch)) { guess = g->find(ch); } else { guess.length = 0; guess.c = 0xffff; guess.error = UINT_MAX; } if ( (predict && (g == 0 || g->isEmpty())) || guess.length != 0) { if (g && g->shift) ch = convertToUpper(ch); if ( set >= maxset-(guess.length > 0 ? guess.length : 1) ) { InputMatcherWordError we; we.text = str+ch; we.error = error; if (guess.error != UINT_MAX) we.error += guess.error; if ( node->isWord() || allowprefix ) r.append(we); } // is >> 10 so can up 2^10 = 1024 char words. if (guess.length) { r += findWords(node->jump(), set+guess.length, maxset, str+ch, error+(guess.error >> 10), allowprefix, predict); } else if (predict) { r += findWords(node->jump(), set+1, maxset, str+ch, error, allowprefix, predict); } }
int solve(FILE *f) { SymbolTable *table = NULL; int i; table = createSymbolTable(makeDomainEntry, compareDomainEntry); getTableData(f, table); do { findWords(table); printf("Do you want to continue?\n"); } while(getContinueRequest() != 'N'); dropSymbolTable(table); return 0; }
void findWords(vector<vector<char> > &board, TrieNode *node, int i, int j, string &sol,vector<string> &res){ if (i < 0 || i >= board.size() || j < 0 || j >= board[0].size()) return; if (board[i][j] == '*' || !node->next[board[i][j]-'a']) return; char tmp = board[i][j]; TrieNode *nextNode = node->next[board[i][j]-'a']; sol.push_back(board[i][j]); if(nextNode->word_end){ res.push_back(sol); nextNode->word_end = false; // avoid the dup } board[i][j] = '*'; findWords(board, nextNode, i + 1, j, sol, res); findWords(board, nextNode, i, j + 1, sol, res); findWords(board, nextNode, i - 1, j, sol, res); findWords(board, nextNode, i, j - 1, sol, res); board[i][j] = tmp; sol.erase(sol.length()-1); }
void testAI(){ Grid<char> board = makeRandomBoard(StandardCubes); DrawBoard(4,4); addLetters(board); Set<string> results; Lexicon lex("lexicon.dat"); findWords(board, lex, results); Set<string>::Iterator itr = results.iterator(); while ( itr.hasNext() ){ string word = itr.next(); cout << "word: " << word << endl; } }
int TextBuffer::applyWordWrapping(std::vector<FONSquad>& _quads, const TextStyle::Parameters& _params, const FontContext::FontMetrics& _metrics, Label::Type _type, glm::vec2* _bbox, std::vector<TextBuffer::WordBreak>& _wordBreaks) { struct LineQuad { std::vector<FONSquad*> quads; float length = 0.0f; }; float yOffset = 0.f, xOffset = 0.f; int nLine = 1; std::vector<LineQuad> lines; if (_params.wordWrap < _params.text.length() && _type != Label::Type::line) { _wordBreaks = findWords(_params.text); } else { for (auto& q : _quads) { _bbox->x = std::max(_bbox->x, q.x1); } } lines.push_back(LineQuad()); // atleast one line // Apply word wrapping based on the word breaks for (int iWord = 0; iWord < int(_wordBreaks.size()); iWord++) { int start = _wordBreaks[iWord].start; int end = _wordBreaks[iWord].end; size_t wordSize = end - start + 1; auto& lastLineQuads = lines[nLine - 1].quads; // Check if quads need to be added to next line? if (iWord > 0 && (lastLineQuads.size() + wordSize) > size_t(_params.maxLineWidth)) { xOffset = 0.0f; auto& quad = _quads[start]; auto& prevQuad = lines[nLine - 1].quads.front(); float baseLength = quad.x0 - prevQuad->x0; yOffset += _metrics.lineHeight; xOffset -= (baseLength); lines.push_back(LineQuad()); nLine++; } for (int i = start; i <= end; i++) { auto& q = _quads[i]; q.x0 += xOffset; q.x1 += xOffset; q.y0 += yOffset; q.y1 += yOffset; lines[nLine - 1].quads.push_back(&q); lines[nLine - 1].length = q.x1; // Adjust the bounding box on x _bbox->x = std::max(_bbox->x, q.x1); } } // Adjust the bounding box on y _bbox->y = _metrics.lineHeight * nLine; float bboxOffsetY = _bbox->y * 0.5f - _metrics.lineHeight - _metrics.descender; // Apply justification for (const auto& line : lines) { float paddingRight = _bbox->x - line.length; float padding = 0; switch(_params.align) { case TextLabelProperty::Align::left: padding = 0.f; break; case TextLabelProperty::Align::right: padding = paddingRight; break; case TextLabelProperty::Align::center: padding = paddingRight * 0.5f; break; } for (auto quad : line.quads) { quad->x0 += padding; quad->x1 += padding; quad->y0 -= bboxOffsetY; quad->y1 -= bboxOffsetY; } } return nLine; }
vector<string> findWords(vector<vector<char>>& board, vector<string>& words) { generateTrie(words); unordered_set<string> res; findWords(board, res); return vector<string>(res.begin(), res.end()); }
int TextBuffer::Builder::applyWordWrapping(std::vector<FONSquad>& _quads, const TextStyle::Parameters& _params, const FontContext::FontMetrics& _metrics) { struct LineQuad { std::vector<FONSquad*> quads; float length = 0.0f; }; float yOffset = 0.f, xOffset = 0.f; int nLine = 1; std::vector<LineQuad> lines; if (_params.wordWrap && _params.maxLineWidth < _params.text.length()) { findWords(_params.text, m_wordBreaks); } else { return 1; } lines.push_back(LineQuad()); // atleast one line float totalWidth = 0.f; // Apply word wrapping based on the word breaks for (int iWord = 0; iWord < int(m_wordBreaks.size()); iWord++) { int start = m_wordBreaks[iWord].start; int end = m_wordBreaks[iWord].end; size_t wordSize = end - start + 1; auto& lastLineQuads = lines[nLine - 1].quads; // Check if quads need to be added to next line? if (iWord > 0 && (lastLineQuads.size() + wordSize) > size_t(_params.maxLineWidth)) { xOffset = 0.0f; auto& quad = _quads[start]; auto& prevQuad = lines[nLine - 1].quads.front(); float baseLength = quad.x0 - prevQuad->x0; yOffset += _metrics.lineHeight; xOffset -= (baseLength); lines.push_back(LineQuad()); nLine++; } for (int i = start; i <= end; i++) { auto& q = _quads[i]; q.x0 += xOffset; q.x1 += xOffset; q.y0 += yOffset; q.y1 += yOffset; lines[nLine - 1].quads.push_back(&q); lines[nLine - 1].length = q.x1; totalWidth = std::max(totalWidth, q.x1); } } // Apply justification for (const auto& line : lines) { float paddingRight = totalWidth - line.length; float padding = 0; switch(_params.align) { case TextLabelProperty::Align::left: padding = 0.f; break; case TextLabelProperty::Align::right: padding = paddingRight; break; case TextLabelProperty::Align::center: padding = paddingRight * 0.5f; break; } for (auto quad : line.quads) { quad->x0 += padding; quad->x1 += padding; } } return nLine; }
QStringList InputMatcher::searchDict(bool allowprefix, bool predict, const QString& language, const QStringList& extradict, int start, int end, uint *me) const { // Search dictionaries. Return list of words matching // this->sets, with the most likely word first. If no // words match, prefixes of the most likely words are // returned. InputMatcherWordErrorList r; QString lang = language; QString lang2; if ( lang.isNull() ) lang = chosenLanguages()[0]; if ( lang[2] == '_' ) { // Non country-specific words lang2 = lang.left(2); } bool prefixequal=true; // XXX could be parameterized if needed uint minError = UINT_MAX; for (int prefix=0; (r.isEmpty() || prefixequal) && prefix<=1; ++prefix) { InputMatcherWordErrorList preferred, added, extra; InputMatcherWordErrorList common, fixed, common2, fixed2; if(!qualifiedSets) preferred = findWords(Qtopia::dawg("preferred").root(),start,end, "", 0, prefix, predict); added = findWords(Qtopia::addedDawg().root(),start,end, "",0, prefix, predict); if(!qualifiedSets) common = findWords(Qtopia::dawg("_common",lang).root(),start,end, "",0, prefix, predict); fixed = findWords(Qtopia::dawg("_words",lang).root(),start,end, "",0, prefix, predict); if ( !lang2.isEmpty() ) { common2 = findWords(Qtopia::dawg("_common",lang2).root(), start,end,"",0, prefix, predict); fixed2 = findWords(Qtopia::dawg("_words",lang2).root(), start,end,"",0, prefix, predict); } uint nerror; for (QStringList::ConstIterator xdit=extradict.begin(); xdit!=extradict.end(); ++xdit) { extra = findWords(Qtopia::dawg(*xdit).root(),start,end,"",0, prefix, predict); nerror = r.merge(extra); if (nerror < minError) minError = nerror; } nerror = r.merge(preferred); if (nerror < minError) minError = nerror; nerror = r.merge(common); if (nerror < minError) minError = nerror; nerror = r.merge(fixed); if (nerror < minError) minError = nerror; nerror = r.merge(added); if (nerror < minError) minError = nerror; nerror = r.merge(common2); if (nerror < minError) minError = nerror; nerror = r.merge(fixed2); if (nerror < minError) minError = nerror; if ( !allowprefix ) break; } QStringList deleted = findWords(Qtopia::dawg("deleted").root(),start,end,"",0, false, predict).asStringList(); for ( QStringList::ConstIterator i=deleted.begin(); i!=deleted.end(); ++i) { r.remove(*i); }; qLog(Input) << "searchDict" << (allowprefix ? " with prefixes" : "") << (predict ? " with prediction" : "") << " in" << language << ": " << r; if (me) *me = minError; // if error larger than X add extra word? not here. return r.asStringList(); }