您的位置:首页 > 其它

POJ 1256 全排列

2013-08-28 16:10 295 查看
题意:重要的是这句话:An upper case letter goes before the corresponding lower case letter. So the right order of
letters is 'A'<'a'<'B'<'b'<...<'Z'<'z'。所以在使用STL中的next_permutation(b.begin(),b.end()时还需判断才可以。

思路:刚开始我确实想用next_permutation(b.begin(),b.end())了,但是发现 'A'<'a'<'B'<'b'<...<'Z'<'z',所以我想到用prev_permutation来得到第一个字符串,但是我实践之后,发现不行,往前取得不到,所以看了Discuss才记得直接对这个字符串进行字母的排列就行了,这个技巧才牛了,用起来可以省很多编程量。

代码如下:

#include<iostream>

#include<string>

#include<cctype>

#include<algorithm>

using namespace std;

bool cmp(char a,char b)

{

if(tolower(a)==tolower(b)) return a<b;

else return tolower(a)<tolower(b);

}

int main()

{

int n;

cin>>n;

while(n--)

{

string a;

cin>>a;

sort(a.begin(),a.end(),cmp);

do

{

cout<<a<<endl;

}while(next_permutation(a.begin(),a.end(),cmp));

}

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: