【发布时间】:2019-10-20 12:44:53
【问题描述】:
我正在尝试构建一个程序来查找单词的所有字谜。这将采用“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;
}
【问题讨论】:
-
优先使用
std::vector<std::string>而不是char数组。 -
你考虑过std::next_permutation吗?
-
srand(time(0)); // Get truly random numbers哈哈 -
// Create array from letters-- 为什么?在整个尝试过程中使用std::string有什么问题? -
rand和srand(time(0))远非真正随机。如果您不打算使用更好的 C++<random>工具,至少在程序开始时只调用一次srand。