C++ 我如何简化这个程序(字谜)?
这是计算字符移位数的算法。我如何简化这一点C++ 我如何简化这个程序(字谜)?,c++,algorithm,anagram,C++,Algorithm,Anagram,这是计算字符移位数的算法。我如何简化这一点 #include <cstdio> #include <vector> #include <iostream> using namespace std; #define SIZE 20 int w[1 << (SIZE + 1)]; 可以通过使用C++提供的算法来简化函数。 在下面的示例代码中,我们读取2个字符串,然后旋转其中一个,然后查看它是否等于另一个。我们将重复该操作,直到找到匹配项,或者直到检测
#include <cstdio>
#include <vector>
#include <iostream>
using namespace std;
#define SIZE 20
int w[1 << (SIZE + 1)];
可以通过使用C++提供的算法来简化函数。 在下面的示例代码中,我们读取2个字符串,然后旋转其中一个,然后查看它是否等于另一个。我们将重复该操作,直到找到匹配项,或者直到检测到不可能进行转换。代码已注释,应该可以理解。如果没有,请询问
#include <iostream>
#include <string>
#include <algorithm>
int main()
{
std::string string1{}; std::string string2{};
std::cout << "Enter 2 strings with same number of digits:\n";
std::cin >> string1 >> string2; // Read strings
// Strings must have same size
if (string1.size() == string2.size()) {
// Countes the number of rotations to the right
size_t rotateCounter{0};
do {
// If rotated string is equal to original, then we found something
if (string1 == string2) break;
// Rotate right
std::rotate(string1.rbegin(),string1.rbegin()+1,string1.rend());
// We have done one more rotation
++rotateCounter;
} while(rotateCounter < string1.size());
// CHeck, if we could find a solution
if ((rotateCounter == string1.size()) && (string1 != string2)) {
std::cout << "Total different strings. No rotation transformation possible\n";
} else {
std::cout << "Number of right shifts needed: " << rotateCounter << '\n';
}
} else {
std::cerr << "Size of strings not equal\n";
}
return 0;
}
最后但并非最不重要。该版本使用纯静态数组和手工制作的旋转算法
这应该足够了。也许下一次,非常明确的要求会有所帮助。然后我们可以选择正确的解决方案
#include <iostream>
#include <algorithm>
#include <iterator>
constexpr size_t MaxDigits = 1000000;
char LetterArray1[MaxDigits] {};
char LetterArray2[MaxDigits] {};
// Rotate array one char to the right
inline void rotateRight(char(&arrayToRotate)[MaxDigits], size_t numberOfLetters)
{
--numberOfLetters;
char temp = arrayToRotate[numberOfLetters];
for (size_t i = numberOfLetters; i > 0; --i) arrayToRotate[i] = arrayToRotate[i - 1];
arrayToRotate[0] = temp;
}
int main() {
// Get the number of letters that the user wants to use
std::cout << "Enter the number of letters: ";
size_t numberOfLetters{ 0 }; std::cin >> numberOfLetters;
// Check input for underflow or overflow
if (numberOfLetters <= MaxDigits)
{
// Now read to strings from the console
std::cout << "\nEnter 2 strings:\n";
std::string inputString1{}; std::string inputString2{};
// Read 2 strings
std::cin >> inputString1 >> inputString2;
// If user enters too short string, we would run into trouble. Therefore, pad string with spaces
inputString1 += std::string(numberOfLetters, ' ');
inputString2 += std::string(numberOfLetters, ' ');
// Copy strings to array
inputString1.copy(LetterArray1, numberOfLetters);
inputString2.copy(LetterArray2, numberOfLetters);
// So, now we have the 2 strings in our arrays
// We will rotate Array1 and compare the result with Array 2. And we count the number of shifts
bool matchFound{ false };
size_t rotateCounter{ 0 };
while (!matchFound && (rotateCounter < numberOfLetters))
{
if (0 == std::memcmp(LetterArray1, LetterArray2, numberOfLetters)) {
matchFound = true; break;
}
rotateRight(LetterArray1, numberOfLetters);
++rotateCounter;
}
if (matchFound) {
std::cout << "\nNecessary rotations: " << rotateCounter << '\n';
}
else {
std::cout << "\nNo Match Found\n";
}
}
else {
std::cerr << "***** Number of letters entered is to big. Max allowed: " << MaxDigits << '\n';
}
return 0;
}
若你们有一个可以运行的程序,只是想知道如何改进它,那个么最好的地方就是简化可读性?使用有意义的变量名称在代码中添加一些说明算法工作原理的文档。欢迎使用StackOverflow。请按照创建此帐户时的建议,遵循帮助文档中的过帐指导原则,和在这里申请。StackOverflow不是设计、编码、研究或教程资源。请检查一般问题。好的,但我应该使用数组和变量N更改字母数。n<1000 000FWIW,需求中给出的最大值表明OP的老师希望不使用动态分配,而是使用问题中所示的静态存储阵列。我不会那么做的。但是嘿,噢,我插入n=4,inputString1=AACB和inputString2=BACA。结果未找到,但应输出:4.Hm?我不明白。如何通过旋转将AACB转换为BACA?逻辑是什么?请ss:AACB->BAAC->CBAA->ACBA->AACB
#include <iostream>
#include <algorithm>
#include <iterator>
constexpr size_t MaxDigits = 1000000;
char LetterArray1[MaxDigits] {};
char LetterArray2[MaxDigits] {};
// Rotate array one char to the right
inline void rotateRight(char(&arrayToRotate)[MaxDigits], size_t numberOfLetters)
{
--numberOfLetters;
char temp = arrayToRotate[numberOfLetters];
for (size_t i = numberOfLetters; i > 0; --i) arrayToRotate[i] = arrayToRotate[i - 1];
arrayToRotate[0] = temp;
}
int main() {
// Get the number of letters that the user wants to use
std::cout << "Enter the number of letters: ";
size_t numberOfLetters{ 0 }; std::cin >> numberOfLetters;
// Check input for underflow or overflow
if (numberOfLetters <= MaxDigits)
{
// Now read to strings from the console
std::cout << "\nEnter 2 strings:\n";
std::string inputString1{}; std::string inputString2{};
// Read 2 strings
std::cin >> inputString1 >> inputString2;
// If user enters too short string, we would run into trouble. Therefore, pad string with spaces
inputString1 += std::string(numberOfLetters, ' ');
inputString2 += std::string(numberOfLetters, ' ');
// Copy strings to array
inputString1.copy(LetterArray1, numberOfLetters);
inputString2.copy(LetterArray2, numberOfLetters);
// So, now we have the 2 strings in our arrays
// We will rotate Array1 and compare the result with Array 2. And we count the number of shifts
bool matchFound{ false };
size_t rotateCounter{ 0 };
while (!matchFound && (rotateCounter < numberOfLetters))
{
if (0 == std::memcmp(LetterArray1, LetterArray2, numberOfLetters)) {
matchFound = true; break;
}
rotateRight(LetterArray1, numberOfLetters);
++rotateCounter;
}
if (matchFound) {
std::cout << "\nNecessary rotations: " << rotateCounter << '\n';
}
else {
std::cout << "\nNo Match Found\n";
}
}
else {
std::cerr << "***** Number of letters entered is to big. Max allowed: " << MaxDigits << '\n';
}
return 0;
}