这是一道全排列的题目,原来经过修改,代码可以写得如此精炼!
/* Poj 1256, Wroter by Dream Chen, 2010/12/12*/
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
char str[20];
int length = 0;
inline bool cmp(char a, char b)
{
if (tolower(a) == tolower(b))
{
return a < b;
}
else
return tolower(a) < tolower(b);
}
int main(void)
{
memset((void*)str,0,sizeof(str));
int n = 0;
scanf("%d",&n);
for (int i = 0; i < n; ++i)
{
scanf("%s",str);
sort(str,str+strlen(str),cmp);
do
{
printf("%s/n",str);
} while (next_permutation(str,str+strlen(str),cmp));
}
return 0;
}
本文介绍了一个精炼的全排列算法实现,通过使用C++标准库函数sort和next_permutation完成字符串的全排列输出,并自定义比较函数确保大小写不敏感的同时维持字典序。

567

被折叠的 条评论
为什么被折叠?



