【问题标题】:C++ Permutation with a output length limit and special arguments具有输出长度限制和特殊参数的 C++ 排列
【发布时间】:2013-04-17 09:02:31
【问题描述】:

我是 C++ 新手,我正在开发一个程序,该程序将生成一个字符串的所有排列的列表,但是我需要能够将输出的长度限制为 5 个字符(这将最可能成为用户设置的可变数字)。我一直在寻找大约一周的时间来寻找类似的东西,我得到的最接近的是以下代码。

来源.cpp:

#include <iostream>;

using namespace std;

void swap(char *fir, char *sec)
{
  char temp = *fir;
  *fir = *sec;
  *sec = temp;
}

/* arr is the string, curr is the current index to start permutation from and size is sizeof the arr */
void permutation(char * arr, int curr, int size)
{
  if(curr == size-1)
  {
    for(int a=0; a<size; a++)
        cout << arr[a] << "";
    cout << endl;
  }

  else
  {
    for(int i=curr; i<size; i++)
    {
        swap(&arr[curr], &arr[i]);
        permutation(arr, curr+1, size);
        swap(&arr[curr], &arr[i]);
    }
  }
}

int main()
{
  string next;
  char str[] = "abcdefghijklmnopqrstuvwxyz1234567890-";

  permutation(str, 0, sizeof(str)-1);
  cin.get();
  cin.get();
}

此代码有效,但它不限制输出的长度。它将输出长度设置为给定字符串的长度。看起来它可能不会在输出中解释多个相同的字母/数字(这我不是 100% 确定)。

此外,我需要设置特殊规则,例如连字符不能是输出中的第一个或最后一个字符。

我试图通过将 sizeof(str)-1 替换为 5 来修改上述代码,但它只会“循环”字符串中的前 5 个字符,因此“e”之外的任何内容都不会被处理。

如果有人能在这方面提供帮助,将不胜感激。

编辑:

感谢大家的出色帮助,我现在将发布我的最终产品,以防其他人也尝试做同样的事情。

最终来源:

#include <iostream>
#include <string>
#include <sstream>
#include <fstream>

using namespace std;

void swap(char *fir, char *sec)
{
  char temp = *fir;
  *fir = *sec;
  *sec = temp;
}

void permutation(char * arr, int size, char* result, int depth, int limit)
{
  ofstream myfile ("permutation.txt", fstream::app);
  if(depth == limit)
  {
    for(int a=0; a<limit; a++){
      myfile << result[a] << "";
      cout << result[a] << "";
    }
    myfile << "\n";
    cout << endl;
  }
  else
  {
    for(int i=0; i<size; i++)
    {
      result[depth] = arr[i];
      permutation(arr, size, result, depth + 1, limit);
    }
  }
  myfile.close();
}

int main()
{
  ofstream myfile ("permutation.txt");
  myfile << "";
  myfile.close();
  string answer;
  char *rArray;
  string startProcess = "N";
  std::cout << "Welcome to permutation v1" << endl;
  std::cout << "-------------------------" << endl;
  std::cout << "Please enter how long the string should be: ";
  std::getline (std::cin,answer);
  int result = atoi(answer.c_str());
  rArray = new char[result];
  std::cout << "\n\nThank You!\n" << endl;
  std::cout << "Please wait, generating possible character array for length of " << result << "." << endl;
  std::cout << "Would you like to proceed? Y = yes & N = no: ";
  std::getline (std::cin,startProcess);
  char str[] = "abcdefghijklmnopqrstuvwxyz1234567890";
  if(startProcess == "Y")
  {
    permutation(str, sizeof(str)-1, rArray, 0, result); 
  }
  else
  {
    std::cout << "\n\nOperation Terminated. No permutations being generated..." << endl;
  }
  cin.get();
  return EXIT_SUCCESS;
}

【问题讨论】:

  • 没有必要实现你自己的swap函数,standard library中有一个。
  • 至于你的问题,你是只想输出字符串的前五个字符,还是输出前五个排列?
  • 这可能是你想做的stackoverflow.com/questions/9217839/…
  • 更新为您真正想要的,而无需输入多个字符

标签: c++ recursion permutation


【解决方案1】:

你需要限制递归的深度

给出字符串中字符的排列,每个只使用一次:

void permutation(char * arr, int currsize, intchar* sizeresult, int depth, int limit)
{
  if(depth == limit)
  {
    for(int a=0; a<limit; a++)
        cout << arr[a]result[a] << "";
    cout << endl;
  }
  else
  {
    for(int i=curr;i=0; i<size; i++)
    {
        swap(&arr[curr],result[depth] &arr[i]);= arr[i];
        permutation(arr, curr+1size, sizeresult, depth + 1, limit);
        swap(&arr[curr], &arr[i]);
    }
  }
}

这样打电话

permutation(str, 0, sizeof(str)-1, result, 0, 5);

对字符串中的字符进行排列,每个字符无限次使用:

void permutation(char * arr, int size, char* result, int depth, int limit)
{
  if(depth == limit)
  {
    for(int a=0; a<limit; a++)
        cout << result[a] << "";
    cout << endl;
  }
  else
  {
    for(int i=0; i<size; i++)
    {
        result[depth] = arr[i];
        permutation(arr, size, result, depth + 1, limit);
    }
  }
}

这样打电话

  char result[5];
  permutation(str, sizeof(str)-1, result, 0, sizeof(result));

【讨论】:

  • 谢谢你,这似乎有点工作,但我很早就担心的似乎是真的。这不允许一个字符在字符串中出现多次。例如:aaaaa、aaaab 它们也是我寻求的有效输出,并且我目前拥有的代码不会在字符串中多次列出这些字符
  • 我可能在不修改代码的情况下修复了它。这可能是非常规的,但如果我在初始数组中添加可能的字符 5 次,它似乎会像它应该打印的那样打印。如果有更好的方法,请告诉我
  • 非常感谢。它就像一个魅力。如果其他人需要,我将在下面发布工作代码。
【解决方案2】:

这实际上并不是一项艰巨的工作。如果您可以在该函数中使用递归和循环,则可以解决它。我建议使用标准库中的next_permutation 函数。

事实就是时间。在 3 秒内,您只能处理 8 个字符的排列。并且条件将取决于要求。假设在您的示例中,如果您需要在开始或结束时省略连字符,则可以进行修剪。

我的实现的伪代码:

    char array[] = "abcdee";
    char eachPerm[6];
    bool usedmatrix[6][6];
    Recur(int depth, int n)
    {
       // you can return from here if this is not the right path, suppose '-'
       // in first or last place.
       if(depth == n)
       {
          print;
       }
       else
       {
          int i;
          for(i= 0 to array.length)
          {
               if(array[i] is not used before)
               eachPerm[depth] = array[i];
               recur(depth+1, n);
          }
       }
    }

最初调用这个函数recur(0, 5)

【讨论】:

    猜你喜欢
    • 2023-03-13
    • 1970-01-01
    • 2016-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多