如何找到所有可能的字谜而无需递归?

时间:2019-06-05 01:17:14

标签: c++ anagram

我正在尝试构建一个程序,以查找单词的所有字谜。这将采用“ 123”并将其变成“ 132”,“ 213”,“ 231”,“ 312”,“ 321”(不相关的顺序)。我已经看过How can I get all the anagrams of a string上的帖子,但是我希望使用普通循环而不是函数递归来实现。到目前为止,我的字谜功能是这样的:

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”,则程序仅返回“ 1234”,“ 1243”,“ 1324”,“ 1342”,“ 1423”和“ 1432”。如果您在其中看不到图案,请注意第一个字符总是相同

我不确定为什么我的程序无法继续使用。完成此操作后,它仅以全速挂起(可能生成已经被猜到的单词并一次又一次地运行)。

运行它的完整代码在这里:

// 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;
}

1 个答案:

答案 0 :(得分:6)

您可以使用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