UVA 195 Anagram
题意:求输入字符串的所有组合,按字典序输出!
解法:使用枚举(枚举前先找出最字符串的最小字典序)枚举时加上枚举生成条件!
1 #include <iostream> 2 #include <string> 3 #include <algorithm> 4 using namespace std; 5 6 bool comp(char ch1,char ch2){ 7 if(ch1 - ch2 == 32){ 8 return false; 9 } 10 if (ch2 - ch1 == 32){ 11 return true; 12 } 13 if(ch1 >= 'a' && ch2 <='Z') 14 { 15 return ch1 - 32 < ch2; 16 } 17 else if(ch1 <='Z' && ch2 >= 'a') 18 { 19 return ch1 + 32 < ch2; 20 } 21 else{ 22 return ch1 < ch2; 23 } 24 25 } 26 // comp() below can also meet our need 27 /** 28 int compute(char ch){ 29 if(ch>='a' && ch<='z') 30 return (ch-'a')*2 +1; 31 else if(ch>='A' && ch<='Z') 32 return (ch-'A')*2; 33 else 34 return 0; 35 } 36 bool comp(char ch1,char ch2){ 37 return compute(ch1)<compute(ch2); 38 } 39 **/ 40 int main() 41 { 42 int in,n; 43 cin>>in; 44 45 while(in--) 46 { 47 string s; 48 cin>>s; 49 n=s.length(); 50 sort(s.begin(),s.end(),comp); 51 do{ 52 cout<<s<<endl; 53 //字典序要注意加入comp()生成条件 54 }while(next_permutation(s.begin(),s.end(),comp)); 55 } 56 return 0; 57 }