1199:全排列

【题目描述】

给定一个由不同的小写字母组成的字符串,输出这个字符串的所有全排列。

我们假设对于小写字母有‘a’ <‘b’ < ... <‘y’<‘z’,而且给定的字符串中的字母已经按照从小到大的顺序排列。

【输入】

只有一行,是一个由不同的小写字母组成的字符串,已知字符串的长度在1到6之间。

【输出】

输出这个字符串的所有排列方式,每行一个排列。要求字母序比较小的排列在前面。字母序如下定义:

已知S=s1s2...sk,T=t1t2...tk

,则S<T等价于,存在p(1≤p≤k),使得s1=t1,s2=t2,...,sp−1=tp−1,sp<tp

成立。

【输入样例】

abc

【输出样例】

abc
acb
bac
bca
cab
cba

【提示】

本题目禁止使用STL及包含可以使用的相关调用。

(注释也不让有,也不能包含相关的头文件)

【来源】

// Created on 2020/2/1

/*#include <iostream>
#include <cstring>
#include <cstdlib>*/
#include <bits/stdc++.h>

using namespace std;

int i,j;
const int maxn=10+5;
char ch[maxn],temp[maxn];
bool judge[maxn];
int cnt;

inline void sequence(int cnt)
{
    if(cnt==strlen(ch))
    {
        temp[strlen(ch)]='\0';
        cout<<temp<<endl;
    }

    for(int i=0;i<strlen(ch);i++)
    {
        if(judge[i]==0)
        {
        judge[i]=1;
        temp[cnt]=ch[i];
        sequence(cnt+1);
        judge[i]=0;
        }
    }
}

int main()
{
    cin>>ch;

    sequence(0);

    return 0;
}

关于全排列系列题解:

https://blog.csdn.net/C_Dreamy/article/details/104130586

发布了156 篇原创文章 · 获赞 8 · 访问量 3884

猜你喜欢

转载自blog.csdn.net/C_Dreamy/article/details/104125195
今日推荐