今天看到一个帖子,才知道STL中next_permutation,prev_permutation可以实现字符的全排序问题,STL有不少好东西啊…,顺便把几道相应的题给切了。
PKU 1731
#include "iostream"
#include "algorithm"
#include "string"
using namespace std;
int n,flag;
char s[201];
int main()
{
while(gets(s))
{
flag=0;
n=strlen(s);
sort(s,s+n); //要先排序
printf("%s/n",s);
while(next_permutation(s,s+n))
printf("%s/n",s);
}
return 0;
}
PKU 1256
#include "iostream"
#include "algorithm"
#include "string"
using namespace std;
int n,flag,test;
char s[201];
//这个要自己定义一个cmp函数
int cmp(char a,char b)
{
if(tolower(a)==tolower(b))
return a < b;
else
return tolower(a) < tolower(b);
}
int main()
{
scanf("%d",&test);
getchar();
while(test--)
{
gets(s);
n=strlen(s);
sort(s,s+n,cmp);
printf("%s/n",s);
while(next_permutation(s,s+n,cmp))
printf("%s/n",s);
}
return 0;
}