POJ 1256:Anagram
Anagram
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 18393 Accepted: 7484
Description
You are to write a program that has to generate all possible words from a given set of letters.
Example: Given the word “abc”, your program should - by exploring all different combination of the three letters - output the words “abc”, “acb”, “bac”, “bca”, “cab” and “cba”.
In the word taken from the input file, some letters may appear more than once. For a given word, your program should not produce the same word more than once, and the words should be output in alphabetically ascending order.
Input
The input consists of several words. The first line contains a number giving the number of words to follow. Each following line contains one word. A word consists of uppercase or lowercase letters from A to Z. Uppercase and lowercase letters are to be considered different. The length of each word is less than 13.
Output
For each word in the input, the output should contain all different words that can be generated with the letters of the given word. The words generated from the same input word should be output in alphabetically ascending order. An upper case letter goes before the corresponding lower case letter.
Sample Input
3
aAb
abc
acba
Sample Output
Aab
Aba
aAb
abA
bAa
baA
abc
acb
bac
bca
cab
cba
aabc
aacb
abac
abca
acab
acba
baac
baca
bcaa
caab
caba
cbaa
Hint
An upper case letter goes before the corresponding lower case letter.
So the right order of letters is ‘A’<’a’<’B’<’b’<…<’Z’<’z’.
题意就是按照字典顺序输出所有字符串。
正常来说,只需要一个sort,一个next_permutation就OK的题,唯一一个值得说明的就是要把A,B,C这样的大写字母穿插到里面,要不然出来的顺序不会是AaBbCc,而是ABCabc。所以需要自己写一个自定义函数cmp用来比较。我这里为了穿插进去,使用了ASCII码+31.5,就使得每一个大写字母刚好穿进了小写字母中。
代码:
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string>
using namespace std;
char s[5000];
bool cmp(char a,char b)
{
double front,behind;
if(a>='A'&&a<='Z')
front = (double)a+31.5;
else
front = (double)a;
if(b>='A'&& b<='Z')
behind = (double)b+31.5;
else
behind = (double)b;
return front<behind;
}
int main()
{
int count;
cin>>count;
while(count--)
{
cin>>s;
sort(s,s+strlen(s),cmp);
do
{
cout<<s<<endl;
}while(next_permutation(s,s+strlen(s),cmp));
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
POJ 1256:Anagram的更多相关文章
- POJ 1146:ID Codes
ID Codes Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6281 Accepted: 3769 Description ...
- POJ 3321:Apple Tree + HDU 3887:Counting Offspring(DFS序+树状数组)
http://poj.org/problem?id=3321 http://acm.hdu.edu.cn/showproblem.php?pid=3887 POJ 3321: 题意:给出一棵根节点为1 ...
- POJ 3252:Round Numbers
POJ 3252:Round Numbers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10099 Accepted: 36 ...
- POJ 1256.Anagram
2015-06-04 问题简述: 输出一串字符的全排列,顺序不同于一般的字母序,而是 A<a<B<b......<Z<z.所以应该重写一个比较函数. 原题链接:http: ...
- poj 1256 Anagram(dfs)
题目链接:http://poj.org/problem?id=1256 思路分析:该题为含有重复元素的全排列问题:由于题目中字符长度较小,采用暴力法解决. 代码如下: #include <ios ...
- poj 1256 Anagram—next_permutation的神奇应用
题意:给你一条字符串,让你输出字符串中字符的全排列,输出的顺序要按它给的奇葩的字典序. 题解:要输出全排列,暴力dfs可以过,但要注意题目的字典序以及相同字符的情况.如果用next_permutati ...
- next_permutation,POJ(1256)
题目链接:http://poj.org/problem?id=1256 解题报告: 1.sort函数是按照ASC11码排序,而这里是按照 'A'<'a'<'B'<'b'<... ...
- POJ 1459:Power Network(最大流)
http://poj.org/problem?id=1459 题意:有np个发电站,nc个消费者,m条边,边有容量限制,发电站有产能上限,消费者有需求上限问最大流量. 思路:S和发电站相连,边权是产能 ...
- POJ 3436:ACM Computer Factory(最大流记录路径)
http://poj.org/problem?id=3436 题意:题意很难懂.给出P N.接下来N行代表N个机器,每一行有2*P+1个数字 第一个数代表容量,第2~P+1个数代表输入,第P+2到2* ...
随机推荐
- 一键设置Fluent环境变量小程序
使用视频教程优酷播放地址: https://v.youku.com/v_show/id_XNDU2MTkwNDg5Mg==.html?spm=a2hzp.8244740.0.0 一键设置环境变量小软件 ...
- Systemverilog for design 笔记(五)
转载请标明出处 第一章 System Verilog过程块.任务和函数 1.1. verilog通用目的always过程块(procedural block)(可综合) always过程块的综合 ...
- 119、Java中String类之通过isEmpty判断是否为空字符串
01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...
- 8.Memcache
1.概述 (1) Memcached是什么 Memcached是一款开源的.高性能的.分布式的内存对象缓存系统 (2) Memcached能干什么 最主要的功能就是:在内存中缓存数据,以减轻数据库负载 ...
- Xcode忽略编译警告
关于本文: 1.说明让Xcode忽略编译警告的目的 2.关于编译警告的开启与关闭的实现 3.顺便借此机会宣传下理解编译原理的大大好处 临时有事,后面更新.
- ISAP 算法
Dinic 算法其实已经足够处理大多数的网络流了,但还不够快.接下来介绍的是最优秀的增广路最大流算法:ISAP(Improve Shortest Argumenting Path).它的时间复杂度上界 ...
- Django线上部署代码修改失效问题
记一次django项目的线上部署维护问题,django+nginx 关于nginx反向代理服务器的介绍这里有一篇博客介绍的比较好:nginx的相关介绍 以及当一次客户端请求发出后,uwsig以及uWS ...
- Linux学习计划中遇到的问题记录
随时更新 Linux安装到网络联通遇到的问题及解决办法 1.命令区分大小写 2..网络连通 ping本地 地址遇到问题 解决办法: https://blog.csdn.net/qq_42074075/ ...
- 【转载】Git设置单个文件上传大小
git单个文件默认大小是50M,超过50M,会给出warning.大于100M会无法提交: 可以通过命令,修改单个文件默认大小(以设置500M以例): git config --global http ...
- routes 学习
对于routes的学习,感觉还是看官方文档理解的比较快,主要说明connect和resource Setting up routes¶ It is assumed that you are using ...