带有输出长度限制和特殊参数的c++排列

C++ Permutation with a output length limit and special arguments

本文关键字:参数 c++ 排列 输出 长度限制      更新时间:2023-10-16

我是c++新手,我正在编写一个程序,该程序将生成字符串的所有排列列表,但是我需要将输出长度限制为5个字符(这很可能成为用户设置的可变数字)。我已经搜索了大约一个星期的东西像这样,我已经得到的最接近的是下面的代码。

Source.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 << "nnThank 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 << "nnOperation Terminated. No permutations being generated..." << endl;
  }
  cin.get();
  return EXIT_SUCCESS;
}

您需要限制递归的深度

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

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));

这其实不是一项艰巨的工作。如果你能在函数中使用递归和循环,你就能解决这个问题。我建议使用标准库中的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)