如何在不递归的情况下找到所有可能的字谜?

How to find all possible anagrams without recursion?

我正在尝试构建一个程序来查找单词的所有变位词。这会将“123”变成“132”、“213”、“231”、“312”、“321”(顺序无关)。我在 处看到了 post,但我希望使用普通循环而不是函数递归来执行此操作。到目前为止,我的 Anagram 函数是这样的:

void inline decodeAnagram(string anagram) {
    srand(time(0)); // Get truly random numbers
    while (amount != possibilities) {
        bool failedCheck = false;
        // Create array from letters
        char splitAnagram[1024];
        strcpy_s(splitAnagram, anagram.c_str());

        // Main loop
        for (int i = anagram.length() - 1; i > 0; i--) {
            int index = rand() % i + 1;
            char letter = splitAnagram[index];
            splitAnagram[index] = splitAnagram[i];
            splitAnagram[i] = letter;
        }

        // Loop to get valid array parts back to string
        string result = "";
        for (int i = 0; i <= anagram.length(); i++) {
            result += splitAnagram[i];
        }

        // Check if value is already in vector
        for (int i = 0; i < guesses.size(); i++) {
            if (result == guesses[i]) {
                failedCheck = true;
                break;
            }
        }
        if (failedCheck == false) { // Value is not already in vector
            guesses.push_back(result);
            amount++;
            cout << result << endl;
        }
    }
}

但是,这只给出了第一个字母的字谜。例如,如果我的字谜是“1234”,则程序只有 returns“1234”、“1243”、“1324”、“1342”、“1423”和“1432”。如果您没有看到其中的模式,请注意第一个字符始终 相同

我不确定为什么我的程序没有进一步运行。完成此操作后,它会全速挂起 运行ning(可能会生成已经猜到的单词并一次又一次地 运行ning)。

运行 的完整代码在这里:

// File created on June 4, 2019

#include <iostream>
#include <Windows.h>
#include <iomanip>
#include <vector>
#include <string>

using namespace std;

bool stop = false;
int amount = 0;
int possibilities = 1;
string anagram;
vector<string> guesses;


void inline decodeAnagram() {
    srand(time(0));
    while (amount != possibilities) {
        bool failedCheck = false;
        // Create array from letters
        char splitAnagram[1024];
        strcpy_s(splitAnagram, anagram.c_str());

        // Main loop
        for (int i = anagram.length() - 1; i > 0; i--) {
            int index = rand() % i + 1;
            char letter = splitAnagram[index];
            splitAnagram[index] = splitAnagram[i];
            splitAnagram[i] = letter;
        }

        // Loop to get valid array parts back to string
        string result = "";
        for (int i = 0; i <= anagram.length(); i++) {
            result += splitAnagram[i];
        }

        // Check if value is already in vector
        for (int i = 0; i < guesses.size(); i++) {
            if (result == guesses[i]) {
                failedCheck = true;
                break;
            }
        }
        if (failedCheck == false) { // Value is not already in vector
            guesses.push_back(result);
            amount++;
            cout << result << endl;
        }
    }
}

int main() {
    // Welcome the user and get the anagram to decode
    cout << "Welcome to the Anagram Decoder!" << endl;
    cout << "What do you want your anagram to be? > ";
    cin >> anagram;
    cout << endl << "Attempting to decode " << anagram << endl;

    for (int i = anagram.length(); i > 0; i--) {
        possibilities = possibilities * i;
    }

    cout << possibilities << " possibilities" << endl;

    clock_t start = clock();
    decodeAnagram();

    cout << "Decoded the anagram " << anagram << " in " << setprecision(2) << fixed << (float)(clock() - start) / CLOCKS_PER_SEC << " seconds." << endl << endl << "That's about " << setprecision(0) << amount / ((float)(clock() - start) / CLOCKS_PER_SEC) << " anagrams per second!" << endl;
    return 0;
}

您可以使用 std::next_permutation:

轻松做到这一点
#include <algorithm>
#include <iostream>
#include <string>

int main()
{
  std::string test = "1234";
  std::sort(test.begin(), test.end());  // sort sequence first
  do
  {
     std::cout << test << "\n";
  } while (std::next_permutation(test.begin(), test.end()));
}   

输出:

1234
1243
1324
1342
1423
1432
2134
2143
2314
2341
2413
2431
3124
3142
3214
3241
3412
3421
4123
4132
4213
4231
4312
4321