int main(){ char charArr[3][3] = {{'x','o','o'},{'o','x','o'},{'x','o','x'}}; char whoWonX = findXDiag(charArr); char whoWonY = findYDiag(charArr); if(whoWonX == 'x'){ puts("x has won diagonally"); } else if(whoWonY == 'o'){ puts("o has won diagonally"); } else{ // x or o is not found diagonally search horizontally findWord(charArr, "xxx", &coordX, &coordY); if(coordX == -1 && coordY == -1){ //x was not found }else{ puts("x has won horizontally or vertically"); } int coordX = -1; int coordY = -1; findWord(charArr, "ooo", &coordX, &coordY); if(coordX == -1 && coordY == -1){ puts("There is a stalemate"); // o was not found }else{ puts("o has won horizontally or vertically"); } return 0; } char findXDiag(char charArr[3][3]){ int arrI = 0; int arrJ = 0; for(arrI; arrI < 3; arrI++){ // search for diagonal matches from left to right. if(charArr[arrI][arrI] == 'x'){ if(charArr[arrI][arrI] == 'x'){ if(charArr[arrI][arrI] == 'x'){ return 'x'; } } } } for(arrJ; arrJ < 3; arrJ++){ if(charArr[2][arrJ] == 'x'){ if(charArr[1][arrJ] == 'x'){ if(charArr[0][arrJ] == 'x'){ return 'x'; } } } } return 'N'; }
bool findWord(vector<vector<char>> &board, vector<vector<bool>> &visited, int row, int col, string &word, int index) { if(index==word.size()) return true; if(row<0 || col<0 || row>=board.size() || col>=board[0].size() || visited[row][col] || board[row][col]!=word[index]) return false; visited[row][col] = true; if(findWord(board, visited, row-1, col, word, index+1)) return true; if(findWord(board, visited, row+1, col, word, index+1)) return true; if(findWord(board, visited, row, col-1, word, index+1)) return true; if(findWord(board, visited, row, col+1, word, index+1)) return true; visited[row][col] = false; return false; }
void findWord(vector<string>& res, vector<vector<char>>& board, Trie *root, int &row, int &col, int i, int j, vector<string>& words) { //if (i < 0 || i > row-1 || j < 0 || j > col-1) return; if (board[i][j] == '#' || root->children[board[i][j]-'a'] == nullptr) return; char temp = board[i][j]; board[i][j] = '#'; if (root->children[temp-'a']->leaf) { res.push_back(words[root->children[temp-'a']->idx]); root->children[temp-'a']->leaf = false; } if (i > 0) findWord(res, board, root->children[temp-'a'], row, col, i-1, j, words); if (i < row-1) findWord(res, board, root->children[temp-'a'], row, col, i+1, j, words); if (j > 0) findWord(res, board, root->children[temp-'a'], row, col, i, j-1, words); if (j < col-1) findWord(res, board, root->children[temp-'a'], row, col, i, j+1, words); board[i][j] = temp; }
//This function initiates the check if a given word is on the board //it compares a word taken from input to new strings built on the board // //It is used in the human solving part bool Board::checkBoard(string word) { string newWord; //check if the word was already entered if (binarySearchVector(word, 0, wordsFound.size() - 1, wordsFound) == true){ cout << word << " already entered." << endl; return false; } char newBoard[MAX_ROWS][MAX_COLUMNS]; //run through each position of the board for (int r = 0; r < numRows; r++) { for (int c = 0; c < numCols; c++) { //make a new board, which will be changed, at each location for (int i = 0; i < numRows; i++) { for (int j = 0; j < numCols; j++){ newBoard[i][j] = theBoard[i][j]; } } //start building the new word from an empty string newWord = ""; //see if the word is on the board, using findWord function bool check = findWord(newBoard, newWord, word, r, c); //if it is found, at it to wordsFound and sort wordsFound if (check == true) { wordsFound.push_back(word); sort(wordsFound.begin(), wordsFound.end()); return check; } } } }
void getWord( word_t *symbol ) { /* Eat the whitespace */ while (workingString[windex] == ' ') windex++; /* Found the end of the string, return EOS */ if (workingString[windex] == 0) { symbol->type = EOS; return; } /* Store the word's address */ symbol->word = &workingString[windex]; /* Walk through word, looking for the end */ while ((workingString[windex] != ' ') && (workingString[windex] != 0)) windex++; /* Null terminate the word (in the string) */ if (workingString[windex] == ' ') { workingString[windex++] = 0; } /* Find the word, and get it's type */ findWord(symbol); return; }
int SFont_AlignedHeight (SFont_Font * Font, int w, int gap, char *text) { char buf[512] = ""; int width = 0; int ww = 0; int len = strlen (text); int i = 0; int y = 0; int nextWord = 0; if (text == NULL) return 0; for (i = 0; i < len; i++) { ww = wordWidth (Font, text, i, nextWord + 1); if ((width + ww) >= w) { width = 0; buf[0] = '\0'; y += Font->Surface->h + gap; } width += ww; copyWord (Font, text, buf, i, nextWord); i = nextWord; nextWord = findWord (Font, text, i + 1); } return y += Font->Surface->h; }
/* * After the manager sends a request to get the host's * state information, the host will respond with a message * which has as its first word "GetHostStateAck", followed by * * - host's physical ID * - host's main directory * - host's network address * - host's neighbor address * - host's packet receive flag * * It displays these on the user's console */ void manDisplayHostState(char buffer[]) { char word[1000]; findWord(word, buffer, 2); printf("Host physical ID: %s\n", word); findWord(word, buffer, 3); printf("Directory: %s\n", word); findWord(word, buffer, 4); printf("Network address: %s\n", word); findWord(word, buffer, 5); printf("Neighbor address: %s\n", word); findWord(word, buffer, 6); printf("New packet received flag: %s\n", word); printf("\n"); }
static void parseName(const char *name, int16_t length) { int16_t start=0, limit, wordLength/*, prevStart=-1*/; Word *word; while(start<length) { /* skip any "noise" characters */ limit=skipNoise(name, start, length); if(start<limit) { /*prevStart=-1;*/ start=limit; } if(start==length) { break; } /* get a word and add it if it is longer than 1 */ limit=getWord(name, start, length); wordLength=(int16_t)(limit-start); if(wordLength>1) { word=findWord(name+start, wordLength); if(word==NULL) { word=addWord(name+start, wordLength); } countWord(word); } #if 0 /* * if there was a word before this * (with no noise in between), then add the pair of words, too */ if(prevStart!=-1) { wordLength=limit-prevStart; word=findWord(name+prevStart, wordLength); if(word==NULL) { word=addWord(name+prevStart, wordLength); } countWord(word); } #endif /*prevStart=start;*/ start=limit; } }
void SFont_WriteAligned (SFont_Font * Font, int x, int y, int w, int gap, int align, char *text) { char buf[512] = ""; int width = 0; int ww = 0; int len = strlen (text); int i = 0; int nextWord = 0; if (text == NULL) return; for (i = 0; i < len; i++) { ww = wordWidth (Font, text, i, nextWord + 1); if ((width + ww) >= w) { switch (align) { case ALEFT: SFont_Write (Font, x, y, buf); break; case ARIGHT: SFont_Write (Font, x + w - width, y, buf); break; case ACENTER: SFont_Write (Font, x + (w - width) / 2, y, buf); break; default: break; } width = 0; buf[0] = '\0'; y += Font->Surface->h + gap; } width += ww; copyWord (Font, text, buf, i, nextWord); i = nextWord; nextWord = findWord (Font, text, i + 1); } switch (align) { case ALEFT: SFont_Write (Font, x, y, buf); break; case ARIGHT: SFont_Write (Font, x + w - width, y, buf); break; case ACENTER: SFont_Write (Font, x + (w - width) / 2, y, buf); break; default: break; } }
static int expectWord(const char *zText, const char *zWord, const char **pzContinue){ const char *zWordStart, *zWordEnd; if( findWord(zText, &zWordStart, &zWordEnd) && ascii_strncasecmp(zWord, zWordStart, zWordEnd - zWordStart)==0 ){ *pzContinue = zWordEnd; return 1; } return 0; }
void HashClass::spellCheck(vector<wordStruct>& Words) { for (int index = 0; index < Words.size(); index++) { if (findWord(Words[index].word)) cout << "The word " << Words[index].word << " is spelled correctly." << endl; else cout << "The word " << Words[index].word << " is spelled incorrectly." << endl; }; cout << "Table Size is" << tableSize << endl; };
bool exist(vector<vector<char> > &board, string word) { if(board.empty() || board[0].empty()) return false; vector<vector<bool>> visited(board.size(), vector<bool>(board[0].size(),false)); for(int i=0; i<board.size(); i++) { for(int j=0; j<board[i].size(); j++) { if(findWord(board, visited, i, j, word, 0)) return true; } } return false; }
void findWord(char* currentWord, const char board[4][4], int position, struct ListNode** HEAD) { int i, j, index; int row = position / 4; int col = position % 4; char* newWord = malloc(sizeof(char) * 33); if (newWord == NULL) { printf("Could not allocate enough memory. Returning.\n"); return; } memset(newWord, 0, 32); char adjacent; strcpy(newWord, currentWord); index = strlen(newWord); for (i = row-1; i <= row+1; i++) { for (j = col-1; j <= col+1; j++) { adjacent = getAdjacentChar(i, j, board); if (adjacent != 0 && !containsChar(adjacent, newWord)) { newWord[index] = board[i][j]; if (isWord(newWord)) { struct ListNode* newNode = malloc(sizeof(struct ListNode)); newNode->word = malloc(strlen(newWord) + 1); if (newNode == NULL || newNode->word == NULL) { printf("Could not allocate enough memory. Returning.\n"); return; } strcpy(newNode->word, newWord); newNode->next = *HEAD; *HEAD = newNode; ++index; } if (isPrefix(newWord)) { findWord(newWord, board, i*4+j, HEAD); } } } } free(newWord); }
void HashClass::addToHash(string word) { if (findWord(word)) // if the word is found, update the frequency { addFrequency(word); // add to frequency of word } else // if the word isn't loaded into hash table yet, add new word { add(word, 0, 0); tableItems++; } }
void findWords(Grid<char> &board, Lexicon &lex, Set<string> &words) { Vector<pointT> moves = makeDeltas(); for (int row = 0; row < board.numRows(); row++) { for (int col = 0; col < board.numCols(); col++) { char ch = board.getAt(row, col); string start(1,ch); pointT position; position.row = row; position.col = col; findWord(start, position, board, lex, moves, words); } } }
bool LMInteriorLevelWordList::getProbWithBackoff( int order , int *words , real *prob ) { LMInteriorLevelWordEntry *word_entry ; // There should be 'order' entries in 'words'. The ordering should be eg. // W3,W2,W1,W4 when order=4 if ( (word_entry = findWord( *words )) != NULL ) return word_entry->getProbWithBackoff( order-1 , words+1 , prob ) ; else { *prob = 0.0 ; return false ; } }
void LMInteriorLevelWordList::addProbEntry( int order , int *words , real prob ) { // 'words' is an array of word indices. There should be 'order' elements // in this array and the ordering should be, eg, [w3 w2 w1 w4] for 4-gram. LMInteriorLevelWordEntry *new_word=NULL ; #ifdef DEBUG if ( (order-1) > level ) error("LMInteriorLevelWordList - addEntry - order of new entry must be <= level\n") ; #endif // Do we have an entry for the word at this level ? if ( (new_word = findWord( *words )) == NULL ) { // No we don't so we need to create an entry and add it to our 'entries' array. entries = (LMInteriorLevelWordEntry **)Allocator::sysRealloc( entries , (n_entries+1)*sizeof(LMInteriorLevelWordEntry *) ) ; new_word = new LMInteriorLevelWordEntry( *words ) ; // If the list is empty, just insert it straight off. Also // do an initial check to see if the word belongs at the end of the array if ( (n_entries == 0) || (new_word->word > entries[n_entries-1]->word) ) { entries[n_entries] = new_word ; } else { // Find the place in the list of words where we want to insert the new word for ( int i=0 ; i<n_entries ; i++ ) { if ( (*words) < entries[i]->word ) { // Shuffle down all words from i onwards and place the // new word in position i. memmove( entries+i+1 , entries+i , (n_entries-i)*sizeof(LMInteriorLevelWordEntry *) ) ; entries[i] = new_word ; break ; } } } n_entries++ ; } // 'new_word' now points to the entry for the word corresponding to this level. // Continue adding our entry. new_word->addProbEntry( level , order-1 , words+1 , prob ) ; }
void manWaitForReply(managerLink * manLink, int cmd) { char reply[1000]; char word[1000]; int length; do { usleep(TENMILLISEC); /* Go to sleep for 10 milliseconds */ length = manReplyReceive(manLink, reply); findWord(word, reply, 1); if (strcmp(word, "DISPLAY")==0) manDisplayReplyMsg(reply); else if (strcmp(word, "GetHostStateAck") == 0) manDisplayHostState(reply); } while(length <= 1); }
//this function recursively checks strings on the board //it tries to match them to an inputed word //it takes as input the newBoard, the new string being build, and the r and c // //it is used in the human solving part bool Board::findWord(char newBoard[MAX_ROWS][MAX_COLUMNS], string newWord, string word, int r, int c) { //continue building the string newWord = newWord + newBoard[r][c]; //check for the case of "q" if (newBoard[r][c] == 'q') { newWord = newWord + 'u'; } //check if the newWord is a substring of the word if (word.substr(0,newWord.length()) != newWord) { return false; } //make the current space a blank character. this elimintes backtracking newBoard[r][c] = ' '; //check that the location is legal and the word isn't too long if (r < 0 || r >= numRows || c < 0 || c >= numCols) { return false; } if (newWord.length() > word.length()) { return false; } //check if you have found the word on the board if (newWord == word) { return true; } bool check2; //recursive call for all 8 directions for (int dx = -1; dx < 2; dx++){ for (int dy = -1; dy < 2; dy++) { //check for a valid direction if (dx == 0 && dy == 0) { continue; } //check that the new direction leads to a place on the board if (r + dx < 0 || r + dx >= numRows || c + dy < 0 || c + dy >= numCols) { continue; } //recursive call check2 = findWord(newBoard, newWord, word, r + dx, c + dy); if (check2 == true) { return true; } } } return check2; }
void LMInteriorLevelWordList::addBackoffEntry( int order , int *words , real bo_wt ) { LMInteriorLevelWordEntry *new_word ; // There should be 'order' entries in 'words' and the order should be // most-recent word first. eg. [w4 w3 w2 w1] for 4-gram #ifdef DEBUG if ( (order > level) || (order < 1) ) error("LMInteriorLevelWordList::addBackoffEntry - order out of range\n") ; #endif if ( (new_word = findWord( *words )) == NULL ) { // No we don't so we need to create an entry and add it to our 'entries' array. entries = (LMInteriorLevelWordEntry **)Allocator::sysRealloc( entries , (n_entries+1)*sizeof(LMInteriorLevelWordEntry *) ) ; new_word = new LMInteriorLevelWordEntry( *words ) ; // If the list is empty, just insert it straight off. Also // do an initial check to see if the word belongs at the end of the array if ( (n_entries == 0) || (new_word->word > entries[n_entries-1]->word) ) { entries[n_entries] = new_word ; } else { // Find the place in the list of words where we want to insert the new word for ( int i=0 ; i<n_entries ; i++ ) { if ( entries[i]->word > new_word->word ) { // Shuffle down all words from i onwards and place the // new word in position i. memmove( entries+i+1 , entries+i , (n_entries-i)*sizeof(LMInteriorLevelWordEntry *) ) ; entries[i] = new_word ; break ; } } } n_entries++ ; } new_word->addBackoffEntry( level , order-1 , words+1 , bo_wt ) ; }
/* * Checks a guess against the game board. * Returns true if guess is valid. * If guess is invalid the error parameter will contain an error. */ bool Boggle::makeGuess(const string& guess, string& error){ if(guess == "1"){ for(auto& word : cpuWords) cout << word << endl; cout << endl; error = "DEBUG."; return false; } if(guess.length() < MIN_WORD_LENGTH){ ostringstream oss; oss << "Word must be at least " << MIN_WORD_LENGTH << " letters long."; error = oss.str(); return false; } if(playerWords.count(guess) != 0){ error = "You've already guessed that word!"; return false; } if(!lexicon.contains(guess)){ error = "That word does not exist."; return false; } if(!findWord(guess)){ error = "Word exists, but not on game board."; return false; } playerScore += scoreFor(guess); playerWords.insert(guess); return true; }
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; }
void hostInitTransmit(hostState * hstate, char word[], char replymsg[]) { if (hstate->sendBuffer.busy == 1) { strcpy(replymsg, "Transmit Aborted: Currently Trasmitting"); return; } if (hstate->sendBuffer.valid == 0) { strcpy(replymsg, "Transmit Aborted: No File to Transmit"); return; } char dest[1000]; int dstaddr; findWord(dest, word, 2); dstaddr = ascii2Int(dest); hstate->sendBuffer.dstaddr = dstaddr; hstate->sendBuffer.busy = 1; hstate->sendBuffer.pos = 0; strcpy(replymsg, "Transmit Started"); }
void Completer::complete(const QString& text, int selStart, int selEnd) { if (text.isEmpty()) return; int wordStart = 0, wordEnd = 0; const QString word = findWord(text, selStart, selEnd, &wordStart, &wordEnd); QStringList candidates; if (word == "/") candidates = CommandParser::availableCommands(); else if (wordStart > 0 && text.at(wordStart - 1) == '/') candidates = filterList(CommandParser::availableCommands(), word); else candidates = filterList(m_item->users(), word); if (m_candidates != candidates) { m_current = 0; m_candidates = candidates; } else { if (!m_candidates.isEmpty()) m_current = (m_current + 1) % m_candidates.count(); else m_current = 0; } if (!m_candidates.isEmpty()) { QString replace = m_candidates.value(m_current); if (word == "/") ++wordStart; QString completion = text; completion.replace(wordStart, selEnd - wordStart, replace); emit completed(completion, selStart, wordStart + replace.length()); } }
int main() { long numCommands; // Get number of commands to accept scanf("%li\n", &numCommands); if (numCommands >= 1 && numCommands <= 100000) { Node* root_ = initNode(); int currNum; int addFunction; int findFunction; char add[4]; strcpy(add, "add"); char find[5]; strcpy(find, "find"); for (currNum = 0; currNum < numCommands; ++currNum) { char input[27]; fgets(input, 27, stdin); char* line = strtok(input, " "); addFunction = strcmp(line, add); findFunction = strcmp(line, find); // If user adds word to trie if (addFunction == 0) { line = strtok(NULL, "\0"); if (line[0] != '\n' && line[0] != '\0') { addWord(root_, line); } // If user is looking for word in trie } else if (findFunction == 0) { line = strtok(NULL, "\0"); if ((root_->numLetters_ > 0) && (line[0] != '\n' && line[0] != '\0')) { printf("%li\n", findWord(root_, line)); } else { printf("0\n"); } } } // Free memory in heap freeMemory(root_); } return 0; }
bool findWord(string sofar, pointT position, Grid<char> &board, Lexicon &lex, Vector<pointT> deltas, Set<string> &acc) { if( validBoggle(sofar, lex) ) { acc.add(sofar); return true; } Vector<pointT> next = successors(position, board, deltas); for (int i = 0; i < next.size(); i++) { pointT nextPt = next[i]; char ch = board.getAt(nextPt.row, nextPt.col); string prefix = sofar + ch; if ( lex.containsPrefix( prefix ) ) { Grid<char> copy = board; copy.setAt(nextPt.row, nextPt.col, 'f'); if ( findWord(prefix, nextPt, copy, lex, deltas, acc) ) return true; } } return false; }
int _tmain(int argc, _TCHAR* argv[]) { pTrieNode pMyTrie = (pTrieNode)malloc(sizeof(TrieNode)); initTrieNode(pMyTrie); //读取单词文本 FILE *fWords = fopen("words.txt","r"); if(fWords == NULL) return 0; char szBuf[MAX_WORD_LEN] = {0}; while(fgets(szBuf,MAX_WORD_LEN,fWords) != NULL) //会读入换行符。。。 { szBuf[strlen(szBuf)-1] = '\0'; addWord(pMyTrie,szBuf); memset(szBuf,0,MAX_WORD_LEN); } fclose(fWords); //查找 findWord(pMyTrie,"gram",true); return 0; }
void HashClass::loadToHash() { double deci = vectorOfWords.size() / 8; //attempts for even distribution of the hash tables. tableSize = ceil(deci); //rounds result up in order to ensure theres enough space. HashTable = new Node*[tableSize]; for (int index = 0; index < tableSize; index++) { HashTable[index] = new Node; HashTable[index]->word = "empty"; HashTable[index]->line = 0; HashTable[index]->column = 0; HashTable[index]->next = NULL; } for (int index = 0; index < vectorOfWords.size(); index++) { if (findWord(vectorOfWords[index].word)) // if the word is found, update the frequency { addFrequency(vectorOfWords[index].word); // add to frequency of word } else // if the word isn't loaded into hash table yet, add new word { add(vectorOfWords[index].word, vectorOfWords[index].line, vectorOfWords[index].column); tableItems++; } } // end for }
/* * Input: A English sentence and the position it takes in the artical * Return: A list of integers represent all uncorrect words * Mark: Your sentence should not contains any punctuation */ QList<int> QMyHashMap::correctSentence(QString s, int startpos) { QList<int> pos; int curpos=startpos; if(s.length()==0) return pos; QTextStream ts(&s,QIODevice::ReadOnly); QString wordstofind; while(!ts.atEnd()) { ts>>wordstofind; qDebug()<<wordstofind<<" --->"; if(findWord(wordstofind)=="") { correctWord(wordstofind); pos.push_back(curpos); } // +1 because the space was ingnore by the stream curpos+=wordstofind.length()+1; } //qDebug()<<pos; return pos; }
struct ListNode * findWords(const char board[4][4]) { int i, j; char* word = malloc(sizeof(char) * 33); struct ListNode* HEAD = malloc(sizeof(struct ListNode*)); if (word == NULL || HEAD == NULL) { printf("Could not allocate enough memory. Returning null.\n"); return NULL; } memset(word, 0, 33); HEAD = NULL; for (i = 0; i < 4; i++) { for (j = 0; j < 4; j++) { word[0] = board[i][j]; findWord(word, board, i*4+j, &HEAD); } } free(word); return HEAD; }