POJ - 1256 Anagram

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;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值