如何简化该程序(字谜)?

时间:2019-06-28 12:06:04

标签: c++ algorithm anagram

这是计算字符移位数的算法。我该如何简化呢?

#include <cstdio>
#include <vector>
#include <iostream>
using namespace std;
#define SIZE 20
int w[1 << (SIZE + 1)];

1 个答案:

答案 0 :(得分: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;
}

编辑:

我用数组创建了第二个版本。我无法想象有人要使用数组的任何原因。也许出于教育目的。但在这里我也会发现它适得其反。无论如何。请参见下面。

并且请注意,std::rotate像所有算法一样也适用于纯数组。

这不是由我编译和测试的!

#include <iostream>
#include <string>
#include <algorithm>
#include <iterator>
#include <vector>
#include <iostream>
#include <string>
#include <algorithm>
#include <cstring>

int main()
{
    std::cout << "Enter number of letters for a string: ";
    size_t numberOfLetters{}; std::cin >> numberOfLetters;
    if (numberOfLetters < 1000000)
    {
        // Create Array of char
        char* charArray1 = new char[numberOfLetters];
        char* charArray2 = new char[numberOfLetters];
        // Read the strings
        std::cout << "Enter s strings with excactly " << numberOfLetters << " digits:\n";
        std::string s1{}, s2{}; std::cin >> s1 >> s2;
        // Padding with spaces 
        s1.insert(0, numberOfLetters, ' ');s2.insert(0, numberOfLetters, ' ');

        // Copy the char Array
        s1.copy(charArray1, numberOfLetters);
        s2.copy(charArray2, numberOfLetters);

        // 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 (0 == std::memcmp(charArray1, charArray2, numberOfLetters)) break;
            // Rotate right
            std::rotate(charArray1,charArray1+numberOfLetters-1,charArray1+numberOfLetters);
            // We have done one more rotation
            ++rotateCounter;
        } while(rotateCounter < numberOfLetters);
        // CHeck, if we could find a solution
        if (std::memcmp(charArray1, charArray2, numberOfLetters))  {
            std::cout << "Total different strings. No rotation transformation possible\n";
        } else {
            std::cout << "Number of right shifts needed: " << rotateCounter << '\n';
        }

        delete [] charArray1;
        delete [] charArray2;
    } else {
        std::cerr << "To many letters\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;
}