您的位置:首页 > 其它

poj-1256 Anagram 全排列

2013-08-14 17:45 447 查看
题目来源: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);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  全排列