全排列实现

1.无重复对象全排列

参考的是《算法竞赛入门》P185
方法是用一个额外的数组A,不断放入对象到这个数组中,直到n个为止。

#include<iostream>
using namespace std;

int total = 0;

void permutation(char* S,char* A,int n,int cur){
    if (cur == n){
        for (int i = 0; i < n; i++){
            cout << A[i];
        }
        total++;
        cout << endl;
    }
    else{
        for (int i = 0; i < n; i++){

            bool ok = true;

            for (int j = 0; j < cur; j++){
                if (S[i] == A[j]){
                    ok = false;
                }
            }

            if (ok){
                A[cur] = S[i];
                permutation(S, A, n, cur + 1);
            }
        }
    }
}


int main()
{   
    char S[] = { '1','2', '3'};
    char *A = new char[5];

    permutation(S, A, 3, 0);

    cout << "total=" << total << endl;

    delete[]A;
    return 0;
}

如果需要按字典序输出所有排列,实际上只需要先将数组S先进行字典序排序,再调用该函数。

网上大多用的是一种基于每次与第一个数交换的方法,来枚举所有排列

#include <cstdio> 
using namespace std;

int n = 0;

void swap(int *a, int *b)
{
    int m;
    m = *a;
    *a = *b;
    *b = m;
}
void perm(int list[], int first, int m)
{
    int i;
    if (first > m)
    {
        for (i = 0; i <= m; i++)
            printf("%d ", list[i]);
        printf("\n");
        n++;
    }
    else
    {
        for (i = first; i <= m; i++)
        {
            if (list[first] != list[i]){
                swap(&list[first], &list[i]);
                perm(list, first + 1, m);
                swap(&list[first], &list[i]);

            }
        }
    }
}
int main()
{
    int list[] = { 1,1, 2};
    perm(list, 0, 2);
    printf("total:%d\n", n);
    return 0;
    }

2.有重复对象的排列

前提是数组S已经有序

#include<iostream>
using namespace std;

int total = 0;

void permutation(char* S,char* A,int n,int cur){
    if (cur == n){
        for (int i = 0; i < n; i++){
            cout << A[i];
        }
        total++;
        cout << endl;
    }
    else{
        for (int i = 0; i < n; i++){
            if (i==0 || S[i] != S[i - 1]){
                int c1 = 0;
                int c2 = 0;

                for (int j = 0; j < n; j++){
                    if (S[j] == S[i]){
                        c1++;
                    }
                }

                for (int j = 0; j < cur; j++){
                    if (A[j] == S[i]){
                        c2++;
                    }
                }

                if (c2 < c1){
                    A[cur] = S[i];
                    permutation(S, A, n, cur + 1);
                }

            }
        }
    }
}



int main()
{   
    char S[] = { '1','2', '2'};
    char *A = new char[3];

    permutation(S, A, 3, 0);

    cout << "total=" << total << endl;

    delete[]A;
    return 0;
}

这里写图片描述

更好的实现方法:

http://blog.csdn.net/gao1440156051/article/details/51919068

class Solution {
public:
    void recursion(vector<int> num, int i, vector<vector<int> > &res) {
        if (i == num.size()) {
            res.push_back(num);
            return;
        }
        for (int k = i; k < num.size(); k++) {
            if (i != k && num[i] == num[k]) continue;
            swap(num[i], num[k]);
            recursion(num, i + 1, res);
        }
    }
    vector<vector<int> > permuteUnique(vector<int> &num) {
        sort(num.begin(), num.end());
        vector<vector<int> >res;
        recursion(num, 0, res);
        return res;
    }
};
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值