字符串全排列

给定一个全由小写字母构成的字符串,求它的全排列,按照字典序从小到大输出。

输入格式:

一行,一个字符串,长度不大于8。

输出格式:

输出所有全排列,每行一种排列形式,字典序从小到大。

样例">输入样例:

在这里给出一组输入。例如:

abc

输出样例:

在这里给出相应的输出。例如:

abc
acb
bac
bca
cab
cba

代码(或者使用C++ STL next_permmutation() 函数):

#include<iostream>
#include<vector>
#include <algorithm>

using namespace std;
// 交换
inline void Swap(char& a, char& b){
    char temp = a;
    a = b;
    b = temp;
}
// 提前,将list[],第 a 位字符,提到 b位置
// 后退,将list[],第 a 位字符,退到 b位置
void Preposition(char list[], int a, int b){
    char B = list[a];
    if(a>b){
        for(int i=a; i>b; i--)
            list[i] = list[i-1];
    }else{
        for(int i=a; i<b; i++)
            list[i] = list[i+1];
    }
    list[b] = B;
}
void Perm(char list[], int k, int m){
    if(k==m){
        for (int i=0; i<=m; i++)
            cout<<list[i];
        cout<<endl;
    }else{
        for (int i=k; i<=m; i++){
            Preposition(list, i, k);
            Perm(list, k+1, m);
            Preposition(list, k, i);
        }
    }
}

int main(){
    string s;
    cin>>s;
    vector<char> temp;
    for(char a:s)
        temp.push_back(a);
    sort(temp.begin(), temp.end());
    char list[8];
    int i = 0;
    for(char a : temp)
        list[i++] = a;
    Perm(list, 0, i-1);
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值