bool searchWord(string word, TrieNode* node, int s) {
     if (s == word.size()) return node->is_string;
     
     if (node->leaves.count(word[s])) return searchWord(word, node->leaves[word[s]], s+1);
     else if (word[s] == '.') {
         for (auto i : node->leaves) {
             if (searchWord(word, i.second, s+1)) return true;
         }
     }
     return false;
 }
 bool searchWord(string &word, TrieNode* node, int p) {
     if(p == word.size())
         return node->iskey;
     
     if(word[p] == '.') {    // 匹配任何字符
         for(int i=0; i<26; ++i)
             if(node->children[i] && searchWord(word, node->children[i], p+1))
                 return true;
         return false;
     }
     else
         return node->children[word[p]-'a'] && searchWord(word, node->children[word[p]-'a'], p+1);
 }
 bool searchWord(const char *ch, TrieNode *r){ //const char *ch contains all value in word
     if(!r) return false;
     if(*ch == '\0') return r->isword;   //check if it is end of the word
     if(*ch == '.'){
         for(int i = 0; i < 26; i++){  //find each of the character
             if(searchWord(ch+1, r->nextNode[i])) return true;  //this should check each of them
         }
         return false;
     }
     else
         return searchWord(ch+1, r->nextNode[*ch-'a']);
     
 }
 bool searchWord(node* root, string& word, int pos) {
     if (pos == word.size()) {
         return root->isWord;
     }
     if (word[pos] == '.') {
         for (auto a : root->next) {
             if (a && searchWord(a, word, pos + 1)) {
                 return true;
             }
         }
         return false;
     } else {
         return root->next[word[pos] - 'a'] && searchWord(root->next[word[pos] - 'a'], word, pos + 1);
     }
 }
 bool searchWord(string word, TrieTree *p, int i) {
     if (i == word.size()) {
         return p->isWord;
     }
     if (word[i] == '.') {
         for (auto &a : p->children) {
             if (a && searchWord(word, a, i + 1)) {
                 return true;
             }
         }
         return false;
     } else {
         return p->children[word[i] - 'a'] && 
                searchWord(word, p->children[word[i] - 'a'], i + 1);
     }
 }
示例#6
0
文件: 练习.c 项目: qq0827/readCPP
int main()
{
	char cmdStr[50] = { '0' };
	char printStr[10240] = { '0' };
	scanf("%s", cmdStr);
	myexec(cmdStr, printStr);

	printf("%s", printStr);

	char searchStr[20];
	scanf("%s", searchStr);

	char *p = searchWord(searchStr, printStr);
	if (p != NULL)
	{
		printf("%s ´æÔÚ", searchStr);
	}
	else
	{
		printf("%s ²»´æÔÚ", searchStr);
	}

	system("pause");
	return 0;
}
    vector<string> wordBreak(string s, unordered_set<string>& wordDict) {
        int len = s.size();
        int *dp = new int[len];
        vector<string> ret;
        memset(dp,0,sizeof(int) * len);

        for(int i = 0; i < len; ++i){
            if(wordDict.find(s.substr(0,i+1)) != wordDict.end()){
                dp[i] = 1;
                continue;
            }
            for(int j = 0; j < i; ++j){
                if(dp[j] && wordDict.find(s.substr(j+1,i-j)) != wordDict.end()){
                    dp[i] = 1;
                    break;
                }
            }
        }

        if(dp[len-1])
            searchWord(ret,s,wordDict,0,"",dp);

        delete[] dp;
        return ret;
    }
示例#8
0
/*
 * Reads file of specified fileName and stores word frequency into
 * wordData linked list. int story indicates which story count to increment.
 */
void readFile(wordData **firstWord, char* fileName, int story) {
    FILE *infile = fopen(fileName, "r");
    char tempWord[WORD_LENGTH];

    while(fscanf(infile, "%s", tempWord) == 1) {
        if (searchWord(*firstWord, cleanWord(tempWord), story) == 0)
            insertWord(firstWord, tempWord, story);
    }
}
 bool searchWord(const TrieNode* root, const string word,int idx){
     
     int k = word[idx] - 'a';
     
     if(root->childNode[k]){
         if (idx == word.length() - 1) {
             return root->childNode[k]->isWord;
         }
         else{
             return searchWord(root->childNode[k], word, idx+1);
         }
     }
     
     return false;
 }
示例#10
0
int main(int argc, char * argv[])
{
    dict_t* head = NULL;
    dict_t* head2 = NULL;
    dict_t* pDict = NULL;
    char ch = '\0';
    //readFile(argv[1], &head);
//readFile("x.dat", &head);
    readTxtFile("dict.txt", &head);
    readTxtFile("dict2.txt", &head2);
    pDict = head;
    while(pDict)
    {

        print_node(pDict);
        printf("\n");
        pDict = pDict->next;
    }
    while(1)
    {
        printf("take a choice:\n1 add a new word\n2 delete a word\n3 search a word\n4 reverse to search a word\nx exit\n");
        scanf("%c", &ch);
        switch(ch)
        {
        case '1':
            addWord(&head);
            break;
        case '2':
            delWord(&head);
            break;
        case '3':
            searchWord(&head);
            break;
        case '4':
            reverseSearchWord(&head);
            break;
        case 'x':
            printf("exit...\n");
            return 0;
            break;
        default:
            continue;
            break;
        }
    }
    //writeFile("x.dat", &head);
    return 0;
}
示例#11
0
 void searchWord(vector<string> &ret, string s, unordered_set<string>& dict,
     int idx, string str, int *dp)
 {
     int len = s.size();
     for(int i = 0; i + idx < len; ++i){
         string substr = s.substr(idx,i+1);
         if(dp[idx+i] && dict.find(substr) != dict.end()){
             if(idx + i < len-1){
                 searchWord(ret,s,dict,idx+i+1,str+substr+" ",dp);
             }else{
                 ret.push_back(str+substr);
                 return ;
             }
         }
     }
 }
示例#12
0
int main(void) {
	char *input[4] = {"thyme","rhyme","me","dime"};	
	int k;
	char *temp;
	newTrie trie;
	trieInitialization(&trie);
	for(k=0; k<4; k++){							
		trieInsert(&trie,input[k]);
	}
	int check = searchWord(&trie,"hello");
	returnRhymes(&trie,"rhyme");
	if(check == 1){
		printf("The word is present in the trie");
	}
	else if(check == 0){
		printf("The word is not present in the trie");
	}
	return 0;
}
示例#13
0
int main ( void )
{
	TreeNode * root = new TreeNode;
	int wordsToCheck;
	char word [ 1010 ];

	scanf ( "%s", word ); // Wyraz.
	addSpecialWord ( root, word ); // Dodaj.

	scanf ( "%d", &wordsToCheck ); // Ilość wyrazów do sprawdzenia.

	for ( int i = 0; i < wordsToCheck; i++ ) // Wyrazy.
	{
		scanf ( "%s", word );
		printf ( "%s\n", ( searchWord ( root, word ) ) ? "TAK" : "NIE" );
	}

	return 0; // Zwracam 0.
}
示例#14
0
文件: TRIE.C 项目: not-jay/Tries-ADT
/*
 * Trie deletion has 3 cases
 *
 * Case 1: The word is a separate branch in the tree and it doesn't have
 *         any other children. Simply delete the starting node.
 * Case 2: The word contains suffices. Only remove the "word" marker
 * Case 3: The reverse of Case 2. The word is a suffix. Delete only the
 *		   suffix
 *
 * This only replaces the deleted node with NULL and then calling the
 * sortChild() function and then realloc()
 *
 * realloc() only fails if size == 0 or if the block cannot be reallocated
 */
void deleteWord(Trie trie, char *str) {
	Node current = trie->dummy, temp = NULL;
	int i, tmp;

	if(!searchWord(trie, str)) return;

	/* Test for case 1*/
	if(testCase(trie, str, case1)) {
		/* Simply remove the node */
		i = findChildID(current, str[0]);
		nodeDelete(current->child[i]);
		resizeChildren(current);
	} else if(testCase(trie, str, case2)) { /* Test for case 2 */
		/* No real deletion occurs here, the nodes would
		 * still be there, but it makes the word unsearchable
		 */
		for(i = 0; i < strlen(str); i++) {
			current = findChild(current, str[i]);
		}
		current->endMarker = false;
	} else if(testCase(trie, str, case3)) { /* Test for case 3 */
		/* Traverse the word and keep track on which node
		 * had children > 1, then find the next matching node
		 * and delete it
		 */
		 temp = trie->dummy;

		 for(i = 0; i < strlen(str); i++) {
			temp = findChild(temp, str[i]);
			if(temp->children > 1) {
				current = temp;
				tmp = i+1;
			}
		 }
		 i = findChildID(current, str[tmp]);
		 nodeDelete(current->child[i]);
		 resizeChildren(current);
	}
}
示例#15
0
SimilarWord::SimilarWord(MainWindow* m)
{
    parent = m;
    this->myRecv = new Recv(parent->getSocket());
    connect(this->myRecv,SIGNAL(similarSignal(QString,QVector<Word>)),this,SLOT(recvMessage(QString,QVector<Word>)));

    lineEdit = new myQLineEdit(parent);
    lineEdit->show();
    lineEdit->setGeometry(100,60,270,40);

    connect(lineEdit,SIGNAL(pressEnterSignal()),this,SLOT(searchWord()));

    button = new QPushButton(parent);
    button->show();
    button->setGeometry(380,60,120,40);
    button->setText("查询亲戚单词");
    connect(button,SIGNAL(clicked(bool)),this,SLOT(searchWord()));


    textEdit = new QTextEdit(parent);
    textEdit->show();
    textEdit->setGeometry(100,110,400,280);
}
示例#16
0
文件: clab.c 项目: klopez9/dictionary
void menu(char ***array, int *wordCount, int *capacity)
{
  int exit;
  char option;
  char word[WORD_LENGTH];
  
  exit = 0;
  do 
  {
    printf("\nMenu\n");
    printf("'S'earch, 'I'nsert, 'R'emove, 'C'ount, 'P'rint, 'Q'uit\n");
    scanf("%c", &option);
    /* takes in char + newline character
       see "newline handler" below
    */
    if (option == 's' || option == 'S')
    {
      /* call search() */
      printf("\nSearch\n");
      searchWord(array, wordCount);
    } 
    else if (option == 'i' || option == 'I') 
    {
      /* call insert() */
      printf("\nInsert\n");
      printf("\nEnter a word: ");
      scanf("%s", word);
      insertWord(array, wordCount, word, capacity);
      printf("\nWord inserted.\n");
    } 
    else if (option == 'r' || option == 'R') 
    {
      /* call remove() */
      printf("\nRemove\n");
      removeWord(array, wordCount);
    } 
    else if (option == 'c' || option == 'C') 
    {
      /* call count() */
      printf("\nCount\n");
      printCount(*wordCount);
    } 
    else if (option == 'p' || option == 'P') 
    {
      printf("\nPrint\n");
      printArray(*array, *wordCount);
    } 
    else if (option == 'q' || option == 'Q') 
    {
      exit = 1;
    } 
    else 
    {
      /* invalid entry */
      printf("\nInvalid Entry, returning to main menu\n");
    }
    /* newline handler */
    while (option != '\n') 
    {
      scanf("%c", &option);
    }
  } 
  while (exit == 0);
}
 // Returns if the word is in the data structure. A word could
 // contain the dot character '.' to represent any one letter.
 bool search(string word) {
     return searchWord(word.c_str(), root);
 }
示例#18
0
文件: monitor.c 项目: pajocos/SOPE
int main(int argc, char *argv[]) {

	if (argc <= 3) {
		fprintf( stderr, "Usage: %s time word file1 file2 ...\n", argv[0]);
		exit(1);
	}

	noFiles = 0;

	int TIME = atoi(argv[1]);
	char* word = argv[2];
	numFiles = argc-3;


	//files
	pid_t temp[numFiles];
	pidsFile = temp;

	unsigned i;
	for(i = 0; i < numFiles; i++) {
		
		pidsFile[i] = fork();

		if(pidsFile[i] < 0) {
			fprintf(stderr, "Error while forking\n");
			exit(1);
		}
		else if (pidsFile[i] > 0) {
			//PARENT
		}
		else {
			//SON
			searchWord(argv[i+3], word);
		}
	}

	pidFileMonitor = fork();

	if(pidFileMonitor < 0) {
		fprintf(stderr, "Error while forking\n");
		exit(1);
	}
	//child process
	else if(pidFileMonitor > 0) {
		//parent process
	}

	else {
		//child process
		char* files[numFiles];


		for(i = 0;i<numFiles;i++)
			files[i] = argv[i+3];

		fileMonitor(files);
	}


	struct sigaction action;

	// prepare the 'sigaction struct'
	action.sa_handler = handler;
	sigemptyset(&action.sa_mask);
	action.sa_flags = 0;
	
	// install the handler
	if (sigaction(SIGUSR1,&action,NULL)<0 || sigaction(SIGUSR2,&action,NULL)<0 || sigaction(SIGCHLD,&action,NULL) < 0) {
		fprintf(stderr, "Error while instaling handlers...");
		exit(1);
	}

	while(TIME > 0 && noFiles == 0)
		TIME = sleep(TIME);

	noFiles = 1;

	if(noFiles == 1)
		processHandler();

	return 0;
}
 // 检索单词,可以包含'.'
 bool search(string word) {
     return searchWord(word, root, 0);
 }
 // Returns if the word is in the trie.
 bool search(string word) {
     if(word.length() == 0) return true;
     
     
     return searchWord(root,word,0);
 }
 // Returns if the word is in the data structure. A word could
 // contain the dot character '.' to represent any one letter.
 bool search(string word) {
     return searchWord(start, word, 0);
 }
 // Returns if the word is in the data structure. A word could
 // contain the dot character '.' to represent any one letter.
 bool search(string word) {
     // Write your code here
     return searchWord(word, root, 0);
 }