您的位置:首页 > 其它

POJ - 1256 Anagram

2016-02-24 19:36 363 查看

1.题面

http://poj.org/problem?id=1256

2.解题思路

这道题定义了一种新的比较规则,然后让我们从小到大输出所有可能的排列,使用STL库中的next_permutation就好了。然后发现next_permutation还是很智能的,在对aaaa这样的序列进行全排列时,它只会产生一个序列,也就是说next_permutation工作的时候,要么直接返回false,否则,它产生的下一个序列必定会比当前序列要大。

这样说突然想研究一些next_permutation的源代码。

还有就是我做到这道题时才发现next_permutation是可以使用cmp函数的,这使我非常震惊,只能说STL我使用地还不是很熟练

3.解题代码

/*****************************************************************
> File Name: tmp.cpp
> Author: Uncle_Sugar
> Mail: uncle_sugar@qq.com
> Created Time: 2016年02月18日 星期四 13时23分28秒
****************************************************************/
# include <cstdio>
# include <cstring>
# include <cmath>
# include <cstdlib>
# include <iostream>
# include <iomanip>
# include <set>
# include <algorithm>
# include <map>
# include <vector>
# include <stack>
# include <cctype>
using namespace std;

const int debug = 1;
const int size  = 5000 + 10;
typedef long long ll;

int weight(char ch){
if (islower(ch))
return int(ch)*2 + 2;
else
return (ch - 'A' + 'a')*2 + 1;
}
bool cmp(char cmper1,char cmper2){
return weight(cmper1) < weight(cmper2);
}
int main()
{
std::ios::sync_with_stdio(false);cin.tie(0);
int i,j,k;
int n;
cin >> n;
string str;
while (n--){
cin >> str;
sort(str.begin(),str.end(),cmp);
do{
cout << str << '\n';
}while (next_permutation(str.begin(),str.end(),cmp));
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: