POJ - 1256 Anagram
2016-02-24 19:36
363 查看
1.题面
http://poj.org/problem?id=12562.解题思路
这道题定义了一种新的比较规则,然后让我们从小到大输出所有可能的排列,使用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; }
相关文章推荐
- hasshmap输出value
- 《大话设计模式》Python版代码实现
- 解决集群Hive表注释为?的编码问题
- Codeforces629 C. Famil Door and Brackets
- 不使用额外存储节点的情况下使单链表逆序问题(配图解)
- codeforces 55D
- 理解UART
- C++ 中关于new的介绍
- centos6.5下vsftpd服务的安装及配置并通过pam认证实现虚拟用户文件共享
- centos6.5下vsftpd服务的安装及配置并通过pam认证实现虚拟用户文件共享
- 接口实现Fragment、Activity间互调解耦
- WIN7启动项删除
- leetcode MajorityElement相关问题
- C语言深度剖析-----数组与指针分析
- 单实例单向rac搭建gg流
- Leetcode之super ugly number
- 今天看到的一个攻击
- linux c Makefile 文件制作(由简至繁)
- <Perl语言入门>读书笔记 | 一. 简介
- Android Init Language