题意:字符的全排列(顺序:'A'<'a'<'B'<'b'<...<'Z'<'z')。
思路:STL中next_permutation的运用,多写一个比较函数。
源代码如下:
#include<iostream>
#include<algorithm>
using namespace std;
const int MAX = 15;
int val(char c) // 按照'A'<'a'<'B'<'b'<...<'Z'<'z'的顺序,每个字母赋一个固定的权值。
{
if(c >= 'A' && c <= 'Z') {
return 2 * (c - 'A');
} else {
return 2 * (c - 'a') + 1;
}
}
bool cmp(char a, char b)
{
return val(a) < val(b);
}
int main()
{
int t;
char str[MAX];
cin >> t;
while(t --)
{
cin >> str;
int n = strlen(str);
sort(str, str + n, cmp);
do
{
cout << str << endl;
}
while(next_permutation(str, str + n, cmp));
}
return 0;
}