コード例 #1
0
ファイル: wordSearchII.cpp プロジェクト: dalanlan/algo
    void wordSearch(vector<vector<char>> &board, int row, int col, int i, int j, TrieNode* root, vector<string> &res) {
        
        if(i < 0 || i>= row || j < 0 || j >= col) {
            return ;
        }
        
        char c=board[i][j];
        
        if(c == '#' || root->child[c-'a'] == NULL) {
            return;
        }
        root = root->child[c-'a'];
        
        if(!root->word.empty()) {
            res.push_back(root->word);
            root->word = "";
        } 
        
        board[i][j] = '#';
        
        wordSearch(board, row, col, i+1, j, root, res);
        wordSearch(board, row, col, i-1, j, root, res);
        wordSearch(board, row, col, i, j+1, root, res);
        wordSearch(board, row, col, i, j-1, root, res);
        board[i][j] = c;
        
        

    }
コード例 #2
0
ファイル: wordSearchII.cpp プロジェクト: dalanlan/algo
    vector<string> findWords(vector<vector<char> > &board, vector<string> &words) {
        // write your code here
        vector<string> res; 
        if(board.size() == 0 || board[0].size() == 0 || words.size() == 0) {
            return res;
        }
        
        TrieNode* root = constructTree(words);
        
        int row = board.size();
        int col = board[0].size(); 
        
        for(int i=0; i<row; i++) {
            for(int j=0; j<col; j++) {
                wordSearch(board, row, col, i, j, root, res);
            }
        }

        return res;
        
    }
コード例 #3
0
ファイル: wfmenu.c プロジェクト: chilininsd/operating_systems
int main(int argc, char **argv)
{	
	HashObjectPtr p;
	char * line = (char *)malloc(sizeof(char)*MAX_INPUT_LENGTH);

	printOptions();
	while (fgets(line, MAX_INPUT_LENGTH, stdin)!=NULL)
	{
		switch (line[0])
		{
			case 'c':
			if (table == NULL)
			{
				if ((table = createNewHashTable()) == NULL)
				{
					printOptions();
					break;
				}
				else
				{
					printf("\n\nSuccess. What would you like to do now?\n\n");
					printOptions();
				}
					
			}
			else 
			{
				FreeHashTable(table);
				if ((table = createNewHashTable()) == NULL)
				{
					printOptions();
					break;
				}
				else
				{
					printf("\n\nSuccess. What would you like to do now?\n\n");
					printOptions();
				}
			}
			break;
			case 'l':
				if (table == NULL)
				{
					printf("You need to first instantiate a hashtable, use the 'c' option\n\n");
					break;
				}
				else 
				{
					if ((p = wordSearch(table)) != NULL)
					{
						printf("\n\nSuccess. What would you like to do now?\n\n");
						printOptions();
					}
					else 
					{
						printOptions();
					}
				}

				break;
			case 'f':
				if (table == NULL)
				{
					printf("You need to first instantiate a hashtable, use the 'c' option\n\n");
					break;
				}
				if (uploadAndRunFile(table) != NULL)
				{
					printf("\n\nSuccess. What would you like to do now?\n\n");
					printOptions();
				}
				
				break;
			case 'p':
				if (table == NULL)
				{
					printf("You need to first instantiate a hashtable, use the 'c' option\n\n");
					break;
				}
				printf("\n\nPrinting Hash Table\n\n");
				PrintHash(table);
				printf("\n\nSuccess. What would you like to do now?\n\n");
				printOptions();
				break;
			case 'r':
				if (table == NULL)
				{
					printf("You need to first instantiate a hashtable, use the 'c' option\n\n");
					break;
				}
				removeThings();
				break;
			case 'q':
				FreeHashTable(table);
				free(line);
				printf("\n\nGoodbye\n\n");
				return 0;
			case 's':
				if (table == NULL)
				{
					printf("You need to first instantiate a hashtable, use the 'c' option\n\n");
					break;
				}

				break;
			default:
				printf("%s\n", "That is an unrecognized entry, please try again\n\n");
				printOptions();
				break;
		}
	}
	free(line);
	return 0;
}
コード例 #4
0
ファイル: door.cpp プロジェクト: evanelias/tournament-trivia
// Displays an entry in a "doormud-style HLP" format file.  This is a simple file format used to store a topic-
// based help system.  This function takes parameters as follows:
//    szFileName     -  Name of the help file
//    szTopic        -  Name of the help entry
//    szError        -  Error message to display if no such entry, or "none" if none.
//    bPartialMatch  -  true if "partial matches" allowed on topic name, if no full matches found.
void displayHelp(char* szFileName, char* szTopic, char* szError, bool bPartialMatch)
{
   ifstream ifsFile;
   short nColor = 7, nLineCount = 0;
   bool bFound = false, bNonstop = false, bSecondPass = false;
   char szLine[90], cKey;
   time_t timeout;

   if ( szFileName == NULL || szTopic == NULL || strlen(szTopic) < 1 )
      return;

   ifsFile.open(szFileName);

   while ( ifsFile )
      {
      ifsFile.getline(szLine, 90);

      // If partial-matches are enabled:  If we don't find a full-match on first pass
      // of the hlp file, we go through it again looking for partial-matches.
      if ( !ifsFile && !bFound && !bSecondPass && bPartialMatch )
         {
         ifsFile.clear();
         ifsFile.seekg(0);
         bSecondPass = true;
         }
      
      if ( szLine == NULL )
         continue;
         
      switch (szLine[0])
         {
         case ';':
            if ( strlen(szLine) > 1 && strcmpi(&szLine[1], szTopic) == 0 )
               bFound = true;
            if ( bSecondPass && strlen(szLine) > 1 && wordSearch(&szLine[1], szTopic) )
               bFound = true;
            break;
         case '-':
            if ( bFound && strlen(szLine) == 1 )
               return;
            if ( bFound && strlen(szLine) > 1 )
               {
               if ( strcmpi(&szLine[1], szTopic) == 0 || szLine[1] == ' ' )
                  return;
               if ( bSecondPass && wordSearch(&szLine[1], szTopic) )
                  return;
               }
            break;
         //case ':':
         //   return;
         case '$':
            if ( bFound && strlen(szLine) > 1 )
               nColor = atoi(&szLine[1]);
            break;
         case '<':
            break;
         case ']':
            if ( strlen(szLine) > 1 && bFound )
               local(&szLine[1], nColor, 0);
            break;            
         default:
            if ( bFound )
               {
               local(szLine, nColor);
 
               if ( ++nLineCount > 20 && !bNonstop)
                  {
                  local("(C)ontinue, (N)onstop, (Q)uit: ", WHITE, 0);
                  timeout = time(NULL);
                  cKey = 0;
                  while ( cKey == 0 )
                     {
                     cKey = inputKey();
                     checkCarrier();
                     checkTimeLeft();
                     inactiveCheck(timeout);
                     Sleep(50);
                     }

                  local(" ");

                  if ( cKey == 'n' || cKey == 'N' )
                     bNonstop = true;

                  if ( cKey == 'q' || cKey == 'Q' || cKey == 'x' || cKey == 'X' || cKey == 's' || cKey == 'S' )
                     return;                        

                  nLineCount = 0;
                  }
               }
            break;
         }
      }

   if ( !bFound && strcmpi(szError, "none") != 0 )
      local(szError);
}