法一:利用next_permutation(s.begin(), s.end())函数法
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
string s;
while (cin>>s) {
cout<< s << endl; //先输出一次原来字符串
for (s.begin(); next_permutation(s.begin(), s.end());) {
cout<< s << endl; //然后利用for和next_permutation函数做到输出全排列
}
}
return 0;
}
方法二:利用递归的办法
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
void backtrace(vector<string>& ans, string s, int start, int len){
if(start == len){
ans.push_back(s);
return;
}
for(int i = start; i < len; i ++){
swap(s[i], s[start]);
backtrace(ans, s, start + 1, len);
}
}
int main(){
string s;
while(cin >> s){
vector<string> ans;
backtrace(ans, s, 0, s.size());
sort(ans.begin(), ans.end());
for(string c : ans)
cout << c << endl;
}
return 0;
}