排列问题
题目描述
输入一个可能含有重复字符的字符串,打印出该字符串中所有字符的全排列.
输入
单组测试数据,输入数据是一个长度不超过10个字符的字符串,以逗号结尾。
输出
打印出该字符串中所有字符的全排列。以字典序顺序输出,用空格分隔。
样例输入
abc,
样例输出
abc acb bac bca cab cba
代码
#include<iostream>
#include<algorithm>
using namespace std;
int main() {
char str[11];
cin.getline(str, 11, ',');
int x = 0;
for (; str[x] != '\0'; x++);
sort(str, str + x);
do {
for (int i = 0; i < x; i++) {
cout << str[i];
}
cout << " ";
} while (next_permutation(str, str + x));
return 0;
}