int search(int matchedWordNumber) { if(matchedWordNumber == encryptedWordNumber) { if(findAnswer) { return 2; } findAnswer = true; for(int i='A';i<='Z';++i) { answer[key[i]] = i; } return 1; } findWordCorrectNumber(); int maxCorrect = -1; int wordIndex; for(int i=0;i<encryptedWordNumber;++i) { if(!matched[i]) { if(correctNumber[i] > maxCorrect) { maxCorrect = correctNumber[i]; wordIndex = i; } } } #if defined(TRIE1) return subSearch(matchedWordNumber, wordIndex, 0, 0); #elif defined(TRIE2) return subSearch(matchedWordNumber, wordIndex, 0, 1); #endif }
size_t fileSearch(const char* filename, const char* text, dictionary* dict) { if (filename == NULL || text == NULL || strlen(filename) == 0 || strlen(text) == 0) { return 1; } FILE* file = fopen(filename, "r"); if (file == NULL) { return 2; } dict = malloc(sizeof(dictionary)); dictionary* dictTemp = dict; char* line = NULL; size_t len = 0; size_t occurances; while(getline(&line, &len, file) != -1) { occurances = subSearch(line, text); /*dictTemp->word = line; dictTemp->num = occurances; dictTemp->next = malloc(sizeof(dictionary)); dictTemp = dictTemp->next;*/ printf("%s %s %zu\n", line, text, occurances); } printf("Closing file\n"); fclose(file); return 0; }
void subSearchTest(const size_t testNum, char *text, const char *pattern, size_t expected) { fprintf(stdout, "%zu - ", testNum); assert(text); assert(pattern); size_t occurances = subSearch(text, pattern); if (occurances == expected) { fprintf(stdout, "Success.\n\n"); } else { fprintf(stdout, "Failure.\n\n"); } }
int subSearch(int matchedWordNumber, int wordIndex, int wordPosition, int trieIndex) { if(wordPosition == encryptedWordLength[wordIndex]) { matched[wordIndex] = true; int temp = search(matchedWordNumber + 1); matched[wordIndex] = false; return temp; } if(!trieIndex) { return 0; } char currentCharacter = encryptedWord[wordIndex][wordPosition]; if(key[currentCharacter]) { #if defined(TRIE1) if(trieNode[trieIndex].next[key[currentCharacter] - 'A']) { if(wordPosition + 1 == encryptedWordLength[wordIndex] && !trieNode[trieNode[trieIndex].next[key[currentCharacter] - 'A']].word) { return 0; } return subSearch(matchedWordNumber, wordIndex, wordPosition + 1, trieNode[trieIndex].next[key[currentCharacter] - 'A']); } return 0; #elif defined(TRIE2) for(;trieIndex;trieIndex=trieNode[trieIndex].right) { if(trieNode[trieIndex].character == key[currentCharacter]) { if(wordPosition + 1 == encryptedWordLength[wordIndex]) { if(!trieNode[trieIndex].word) { return 0; } } return subSearch(matchedWordNumber, wordIndex, wordPosition + 1, trieNode[trieIndex].child); } } return 0; #endif } else { int find = 0; #if defined(TRIE1) for(int i=0;i<26;++i) { if(!used[i + 'A'] && trieNode[trieIndex].next[i]) { if(wordPosition + 1 == encryptedWordLength[wordIndex] && !trieNode[trieNode[trieIndex].next[i]].word) { continue; } key[currentCharacter] = i + 'A'; used[i + 'A'] = true; int temp = subSearch(matchedWordNumber, wordIndex, wordPosition + 1, trieNode[trieIndex].next[i]); used[i + 'A'] = false; key[currentCharacter] = 0; if(temp == 2) { return temp; } else if(temp == 1) { find = 1; } } } #elif defined(TRIE2) for(;trieIndex;trieIndex=trieNode[trieIndex].right) { if(!used[trieNode[trieIndex].character]) { if(wordPosition + 1 == encryptedWordLength[wordIndex]) { if(!trieNode[trieIndex].word) { continue; } } used[trieNode[trieIndex].character] = true; key[currentCharacter] = trieNode[trieIndex].character; int temp = subSearch(matchedWordNumber, wordIndex, wordPosition + 1, trieNode[trieIndex].child); key[currentCharacter] = 0; used[trieNode[trieIndex].character] = false; if(temp == 2) { return temp; } else if(temp == 1) { find = 1; } } } #endif return find; } }