题意:求输入字符串的所有组合,按字典序输出!
解法:使用枚举(枚举前先找出最字符串的最小字典序)枚举时加上枚举生成条件!
#include <iostream>
#include <string>
#include <algorithm>
using namespace std; bool comp(char ch1,char ch2){
if(ch1 - ch2 == ){
return false;
}
if (ch2 - ch1 == ){
return true;
}
if(ch1 >= 'a' && ch2 <='Z')
{
return ch1 - < ch2;
}
else if(ch1 <='Z' && ch2 >= 'a')
{
return ch1 + < ch2;
}
else{
return ch1 < ch2;
} }
// comp() below can also meet our need
/**
int compute(char ch){
if(ch>='a' && ch<='z')
return (ch-'a')*2 +1;
else if(ch>='A' && ch<='Z')
return (ch-'A')*2;
else
return 0;
}
bool comp(char ch1,char ch2){
return compute(ch1)<compute(ch2);
}
**/
int main()
{
int in,n;
cin>>in; while(in--)
{
string s;
cin>>s;
n=s.length();
sort(s.begin(),s.end(),comp);
do{
cout<<s<<endl;
//字典序要注意加入comp()生成条件
}while(next_permutation(s.begin(),s.end(),comp));
}
return ;
}