使用 C++ 的排列和/组合

Permutations &/ Combinations using c++

我的代码需要不同版本的排列。我可以实现我想要的,但它不够通用。我的算法随着我的要求不断变大。但这不应该。

这不是任何人的家庭作业,我的一个关键项目需要它,想知道 boost 或其他任何可用的预定义算法。

以下是 next_permutation 使用 c++ 的标准版本。

// next_permutation example
#include <iostream>     // std::cout
#include <algorithm>    // std::next_permutation

int main () 
{
  int myints[] = {1,2,3};
  do 
  {
    std::cout << myints[0] << ' ' << myints[1] << ' ' << myints[2] << '\n';
  } while ( std::next_permutation(myints,myints+3) );


  return 0;
}

给出以下输出:

1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1

但我的要求是:- 假设我有 1 到 9 个数字: 1,2,3,4,5,6,7,8,9

而且我需要可变长度的排列,并且仅按升序排列且没有重复。

假设我需要 3 位长度的排列然后我需要如下输出。

123
124
125
.
.
.
128
129
134   // After 129 next one should be exactly 134
135      // ascending order mandatory
136
.
.
.
148
149
156   // exactly 156 after 149, ascending order mandatory
.
.
.
489   // exactly 567 after 489, because after 3rd digit 9, 2nd digit
567   // will be increased to 49? , so there is no possibility for
.     // 3rd digit, so first digit gets incremented to 5 then 6 then
.     // 7, in ascending order.
.     
.
.
789   // and this should be the last set I need.

我的列表可能包含多达几百个数字,可变长度可以是 1 到列表的大小。

我自己的算法适用于特定的可变长度和特定的大小,当它们都发生变化时,我需要编写大量代码。所以,寻找一个通用的。

我什至不确定这是否称为排列,或者这种 math/logic 有不同的名称。

提前致谢。 马斯克的

形式上,您想生成集合 [0;n-1] 的所有 m-combinations

#include <iostream>
#include <vector>

bool first_combination (std::vector<int> &v, int m, int n)
{
    if ((m < 0) || (m > n)) {
        return false;
    }

    v.clear ();
    v.resize (m);
    for (int i = 0; i < m; i++) {
        v[i] = i;
    }

    return true;
}

bool next_combination (std::vector<int> &v, int m, int n)
{
    for (int i = m - 1; i >= 0; i--) {
        if (v[i] + m - i < n) {
            v[i]++;
            for (int j = i + 1; j < m; j++) {
                v[j] = v[j - 1] + 1;
            }
            return true;
        }
    }

    return false;
}

void print_combination (const std::vector<int> &v)
{
    for (size_t i = 0; i < v.size(); i++) {
        std::cout << v[i] << ' ';
    }
    std::cout << '\n';
}

int main ()
{
    const int m = 3;
    const int n = 5;

    std::vector<int> v;

    if (first_combination (v, m, n)) {
        do {
            print_combination (v);
        } while (next_combination (v, m, n));
    }
}

您可以使用此代码和链接的文章作为灵感。

这个任务可以用一个简单的迭代算法来完成。只需递增第一个可以递增的元素并重新缩放它之前的元素,直到没有要递增的元素。

int a[] = {0,1,2,3,4,5,6,7,8,9}; // elements: must be ascending in this case
int n = sizeof(a)/sizeof(int);
int digits = 7; // number of elements you want to choose
vector<int> indexes; // creating the first combination
for ( int i=digits-1;i>=0;--i ){
    indexes.push_back(i);
}

while (1){
    /// printing the current combination
    for ( int i=indexes.size()-1;i>=0;--i ){
        cout << a[indexes[i]] ;
    } cout << endl;
    ///
    int i = 0;
    while ( i < indexes.size() && indexes[i] == n-1-i ) // finding the first element
        ++i;                                            // that can be incremented
    if ( i==indexes.size() ) // if no element can be incremented, we are done
        break;
    indexes[i]++; // increment the first element
    for ( int j=0;j<i;++j ){ // rescale elements before it to first combination
        indexes[j] = indexes[i]+(i-j);
    }
}

输出:

0123456
0123457
0123458
0123459
0123467
0123468
0123469
0123478
0123479
0123489
0123567
0123568
0123569
0123578
0123579
0123589
0123678
0123679
0123689
0123789
0124567
0124568
0124569
0124578
0124579
0124589
0124678
0124679
0124689
0124789
0125678
0125679
0125689
0125789
0126789
0134567
0134568
0134569
0134578
0134579
0134589
0134678
0134679
0134689
0134789
0135678
0135679
0135689
0135789
0136789
0145678
0145679
0145689
0145789
0146789
0156789
0234567
0234568
0234569
0234578
0234579
0234589
0234678
0234679
0234689
0234789
0235678
0235679
0235689
0235789
0236789
0245678
0245679
0245689
0245789
0246789
0256789
0345678
0345679
0345689
0345789
0346789
0356789
0456789
1234567
1234568
1234569
1234578
1234579
1234589
1234678
1234679
1234689
1234789
1235678
1235679
1235689
1235789
1236789
1245678
1245679
1245689
1245789
1246789
1256789
1345678
1345679
1345689
1345789
1346789
1356789
1456789
2345678
2345679
2345689
2345789
2346789
2356789
2456789
3456789