题目来源:http://poj.org/problem?id=1256
题目大意是求字符串的全排列 要求按A<a<B<b<...<Z<z 的顺序输出
那就把给定的字符串先排序
然后通过STL的next_permutation函数生成全排列(C++)
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
bool cmp(char a, char b)
{
int asc1, asc2;
asc1 = (a >= 'a' && a <= 'z') ? (a - 'a')*2 + 1 : (a - 'A')*2;
asc2 = (b >= 'a' && b <= 'z') ? (b - 'a')*2 + 1 : (b - 'A')*2;
return asc1 < asc2;
}
int main()
{
int total, len;
char str[13];
scanf("%d", &total);
while(total--)
{
scanf("%s", str);
len = strlen(str);
sort(str,str+len,cmp);
printf("%s\n", str);
while(next_permutation(str, str+len,cmp))
printf("%s\n", str);
}
}