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* ...
随机推荐
- Charles + Android 抓取Https数据包 (适用于Android 6.0及以下)
通过Charles代理,我们能很轻易的抓取手机的Http请求,因为Http属于明文传输,所以我们能直接获取到我们要抓取的内容.但是Https内容本身就是加密的,这时我们会发现内容是加密的了.本文我们来 ...
- SignalR Connection has not been fully initialized
在使用 SignalR 过程中遇到 SignalR: Connection has not been fully initialized. Use .start().done() or .start( ...
- 「AHOI2008」紧急集合/聚会
题目描述 这次也是很长的题面啊\(qwq\) 题目大意如下: 给定一棵\(N\)个节点的树以及\(M\)次询问,每次询问给出\(x,\ y,\ z\)三个节点,程序需要在树上找一个点\(p\) 使得\ ...
- 对于JAVA语言的一点理解
java作为一门面向对象的语言,现在常常被用于企业服务器端的后台开发.同时,C语言可能更多地是用于嵌入式的开发,所谓的嵌入式就是航天飞机上的设备软件之类的东西.但是,我逐渐发现,我们平时所说的java ...
- 跨服务器的SQL语句如何书写
select * into 本地库名..表名 from OPENDATASOURCE( 'SQLOLEDB', 'Data ...
- iOS下JS与OC互相调用
背景情况: app项目中有几个界面是需要经常变动的(不仅是内容还有UI布局等),比如活动宣传界面就是属于这一类.但是由于AppStore提交审核也是需要时间的,就算审核快,也不至于每次都为了这点事频繁 ...
- pycharm安装PyQt框架
首先需要安装必要插件 File --> Settings --> Project Interpreter 如果出现以下这种情况:请升级pip,参考:https://www.cnbl ...
- LeetCode 804 唯一摩尔斯密码词
package com.lt.datastructure.Set; import java.util.TreeSet; /* * 一个摩斯码,对应一个字母.返回我们可以获得所有词不同单词翻译的数量. ...
- P1095 解码PAT准考证
1095 解码PAT准考证 (25分) PAT 准考证号由 4 部分组成: 第 1 位是级别,即 T 代表顶级:A 代表甲级:B 代表乙级: 第 2~4 位是考场编号,范围从 101 到 999: ...
- window和document的区别理解,bom和dom的区别理解
Window对象: 是整个BOM的核心,所有对象和集合都以某种方式回接到window对象.Window对象表示整个浏览器窗口,但不必表示其中包含的内容. Document对象: 实际上是window对 ...