1384 全排列
给出一个字符串S(可能有重复的字符),按照字典序从小到大,输出S包括的字符组成的所有排列。例如:S = "1312",
输出为:
1123
1132
1213
1231
1312
1321
2113
2131
2311
3112
3121
3211
收起
输入
输入一个字符串S(S的长度 <= 9,且只包括0 - 9的阿拉伯数字)
输出
输出S所包含的字符组成的所有排列
输入样例
1312
输出样例
1123 1132 1213 1231 1312 1321 2113 2131 2311 3112 3121 3211
#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <string>
#include <algorithm>
using namespace std ;
const int MAX= 10005 ;
char str[MAX] ;
int a[MAX] ;
int n ;
int vis[MAX];
int p[MAX];
map<string,int> mp ;
void dfs(int v){
if(v == n ){
string s = "" ;
for(int i = 0 ; i<n ; i++ ){
s+=p[i]+'0' ;
//printf("%d",p[i]);
}
cout<<s<<endl;
return ;
}
for(int i = 0 ; i<n ;i++ ) {
if(!vis[i]){
vis[i] = 1 ;
p[v] = a[i] ;
dfs(v+1) ;
vis[i] = 0 ;
while(i-1<n && a[i] == a[i+1]) i++ ;
}
}
}
int main(){
cin >> str ;
n = strlen(str);
for(int i = 0 ;i<n ; i++ ) {
a[i] = str[i]-'0' ;
}
sort(a,a+n);
dfs(0) ;
return 0 ;
}
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
main()
{
char s[1005];
int len;
scanf("%s",s);
len=strlen(s);
sort(s,s+len);
do{
puts(s);
}while(next_permutation(s,s+len));
}