我试图使用递归并不断出现溢出错误。我不知道该怎么做。
我想用所有的字谜提供我的一个功能,但我不确定如何。我无法将所有东西都融入到一个函数中,所以我做了两个:一个制作字谜,一个搜索字典以查找所有匹配。但它不起作用,我不知道还能做什么。在我的anagram函数中,我想返回到我的置换函数,但我不能返回一个字符串。我需要它什么都不返回,然后回到功能。
#include <iostream>
#include <fstream>
#include <istream>
#include <string>
using namespace std;
const int MAXRESULTS = 20; // Max matches that can be found
const int MAXDICTWORDS = 30000; // Max words that can be read in
const int MAXPERMUTATIONS = 720; //enough permutations for a 6 letter word
//Beginning of assignment functions
//Read Dictionary function that uses recursion
int readDictionary(istream &, string[]);
//Permuation function
int recursivePermute(string, const string[], int, string[]);
//Print function
void recurPrint(const string[], int);
//swap characters in a string
void swap(string*, string*);
//permutation function
string Permutator(string, int, int, int);
//
//End of Assignment functions
int main()
{
string Permutations[MAXPERMUTATIONS];
string results[MAXRESULTS];
string dict[MAXDICTWORDS];
ifstream dictfile; // file containing the list of words
int nwords; // number of words read from dictionary
string word;
dictfile.open("words.txt");
if (!dictfile) {
cout << "File not found!" << endl;
return (1);
}
nwords = readDictionary(dictfile, dict);
cout << "Please enter a string for an anagram: ";
cin >> word;
//Make all the permutations and store them in an array
int numMatches = recursivePermute(word, dict, nwords, results);
if (numMatches == 0)
cout << "No matches found" << endl;
else
recurPrint(results, numMatches);
}
/***************************************************************************************************
Name: readDictionary
input: ifstream reference, string array
Description: This function returns the number of words added into the array from the dictionary.
****************************************************************************************************/
int readDictionary(istream &file, string DicArr[])
{
int counter = 0;
if (counter > MAXDICTWORDS)
return counter;
if (getline(file, DicArr[0]))
{
counter++;
return counter += readDictionary(file, DicArr + 1);
}
else
return counter;
}
/*****************************************************************************************************
Name: recursivePermute
Input: string, const string array, int, string array
Description: Places all the permutations of word, which are found in dict into results.
Returns the number of matched words found. This number should not be larger than
MAXRESULTS since that is the size of the array. The size is the number of words
inside the dict array.
*******************************************************************************************************/
int recursivePermute(string word, const string dict[], int size, string results[])
{
//count to iterate through the dictionary array and keep in bounds
//numresults to keep track of the number of results
int numResults = 0;
//if statement to if the number of results goes over the limit
if (numResults > MAXRESULTS)
return numResults;
if (size == 0)
return numResults;
//if there is a match check the dictionary
if (word == dict[0])
{
results[0] = word;
numResults++;
}
numResults += recursivePermute(Permutator(word, 0, word.length() - 1, 0), dict + 1, size - 1, results);
return numResults;
}
/*******************************************************************************************************
Name: recurPrint
Input:const string array, int
Description: Prints out the results
*********************************************************************************************************/
void recurPrint(const string results[], int size)
{
if (size == 1)
{
cout << "matching word \"" << results[0] << "\" found!\n" << endl;
return;
}
if (size == 0)
return;
cout << results[size - 1];
recurPrint(results, size - 1);
}
/****************************************************************************************************
name: swap
input: string pointer
description: This functions swaps two characters in a string
*****************************************************************************************************/
void swap(string* a, string* b)
{
string temp;
temp = *a;
*a = *b;
*b = temp;
}
/******************************************************************************************************
********************************************************************************************************/
string Permutator(string word, int beg, int end, int count)
{
string a;
if (count == end)
return word;
if (beg == end)
return;
if(count <= end)
{
swap(word[beg], word[count]);
Permutator(word, beg + 1, end, count);
swap(word[beg], word[count]);
Permutator(word, beg, end, count + 1);
}
}
/******************************************************************************************************
*******************************************************************************************************/
好的,所以我已经缩小了我的问题范围。我遇到了这个函数的问题,我用它来将每个排列提供给我的另一个函数,该函数将检查每个字符串的字符串排列。
string Permutator(string word, int beg, int end, int count)
{
if (count == end)
return word;
if (beg == end)
if (count <= end)
{
swap(word[beg], word[count]);
Permutator(word, beg + 1, end, count);
swap(word[beg], word[count]);
}
}
如果它是无效的,这将有效,但我需要它返回一个字符串,但是当我更改返回类型时,我的整个算法都没有了。在这个赋值中只能是递归的循环不能按预期工作。我的想法不确定我还能做些什么。
答案 0 :(得分:0)
std::map<std::string, std::vector<std::string>>
似乎合适:
std::vector<std::string> allWords /*= */;
std::map<std::string, std::vector<std::string>> dictionary;
for (const std::string& word : allWords) {
auto w = word;
std::sort(w.begin(), w.end());
dictionary[w].push_back(word);
}
然后
std::vector<std::string>
find_words_from_anagram(const std::map<std::string, std::vector<std::string>>& dictionary,
const std::string& word)
{
auto w = word;
std::sort(w.begin(), w.end());
auto it = dictionary.find(w);
if (it == dictionary.end()) {
return {}; // No match.
}
return it->second;
}
Trick是规范化条目,而不是检查所有排列。