poj 1256 按一定顺序输出全排列(next_permutation)

Sample Input

3
aAb
abc
acba
Sample Output

Aab
Aba
aAb
abA
bAa
baA
abc
acb
bac
bca
cab
cba
aabc
aacb
abac
abca
acab
acba
baac
baca
bcaa
caab
caba
cbaa

对字符串进行全排列,字符的大小规则: 'A'<'a'<'B'<'b'<...<'Z'<'z'.

 

 1 # include <iostream>  
 2 # include <cstring>
 3 # include <algorithm>  
 4 using namespace std;  
 5   
 6   
 7 bool cmp(const char &a, const char &b)
 8    {
 9     if (tolower(a) == tolower(b))  //把字符转换成小写字母,非字母字符不做出处理 
10    {
11        return a < b;
12     }
13     else
14        return tolower(a) < tolower(b);
15 
16 }
17   
18 int main()  
19 {  
20     int num = 0;  
21     cin >> num;  
22     while(num--)  
23     {  
24         char str[100];  
25         cin >> str;  
26         int length = strlen(str);  
27         sort(str, str+length, cmp);  
28         cout << str << endl;  
29         while(next_permutation(str, str + length, cmp))  //全排列 
30         {  
31             cout << str << endl;  
32         }  
33     }  
34     return 0;  
35 }  
View Code

 

posted @ 2015-05-12 17:15  __Meng  阅读(257)  评论(0编辑  收藏  举报