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的更多相关文章

  1. POJ 1146:ID Codes

    ID Codes Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6281 Accepted: 3769 Description ...

  2. 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 ...

  3. POJ 3252:Round Numbers

    POJ 3252:Round Numbers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10099 Accepted: 36 ...

  4. POJ 1256.Anagram

    2015-06-04 问题简述: 输出一串字符的全排列,顺序不同于一般的字母序,而是 A<a<B<b......<Z<z.所以应该重写一个比较函数. 原题链接:http: ...

  5. poj 1256 Anagram(dfs)

    题目链接:http://poj.org/problem?id=1256 思路分析:该题为含有重复元素的全排列问题:由于题目中字符长度较小,采用暴力法解决. 代码如下: #include <ios ...

  6. poj 1256 Anagram—next_permutation的神奇应用

    题意:给你一条字符串,让你输出字符串中字符的全排列,输出的顺序要按它给的奇葩的字典序. 题解:要输出全排列,暴力dfs可以过,但要注意题目的字典序以及相同字符的情况.如果用next_permutati ...

  7. next_permutation,POJ(1256)

    题目链接:http://poj.org/problem?id=1256 解题报告: 1.sort函数是按照ASC11码排序,而这里是按照 'A'<'a'<'B'<'b'<... ...

  8. POJ 1459:Power Network(最大流)

    http://poj.org/problem?id=1459 题意:有np个发电站,nc个消费者,m条边,边有容量限制,发电站有产能上限,消费者有需求上限问最大流量. 思路:S和发电站相连,边权是产能 ...

  9. POJ 3436:ACM Computer Factory(最大流记录路径)

    http://poj.org/problem?id=3436 题意:题意很难懂.给出P N.接下来N行代表N个机器,每一行有2*P+1个数字 第一个数代表容量,第2~P+1个数代表输入,第P+2到2* ...

随机推荐

  1. 「CF1303C Perfect Keyboard」

    前置芝士 图的遍历:通过DFS或者BFS遍历全图. 前向星:用来存边,但是在本题用也可以用一个二维数组解决. 具体做法 先从判断YES和NO开始,可以发现如果一个字母与三个及以上不同的字母相邻时肯定是 ...

  2. 爬虫实战_爬取豆瓣图书利用csv库存储

    读取csv文件 通过csv.reader()和DictReader()两个函数 reader()函数返回一个迭代器 会包含表头 通过next函数可以跳过,但是它只能通过下标访问数据: DictRead ...

  3. js加密(十一)yhz566 md5

    1. http://www.yhz566.com/ 2. 登录加密 3. navigator = {}; var rng_psize = 256; var b64map = "ABCDEFG ...

  4. 123、Java面向对象之引用传递实例一

    01.代码如下: package TIANPAN; class Message { private int num = 10; // 定义int基本类型的属性 public Message(int n ...

  5. 使用mybase、Typora搭配坚果云实现个人云笔记

    如果我们没有使用印象笔记.有道云之类的云笔记,那么就会遇到一个问题,比如我在公司是用的公司的电脑,然后下班回家用的自己的电脑,那么我在公司写的文档,比如markdown 文件,mybase知识管理工具 ...

  6. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 表格:悬停表格

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  7. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 表单:表单控件大小

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  8. 一个Win32程序的进化------转载

    一个Win32程序的进化 转载 weixin_34273046 发布于2017-11-15 10:53:00 阅读数 10 收藏 展开 一.为什么要学Win32     要回答这个问题,我们就要先搞清 ...

  9. Django链接MySQL,数据库迁移

    form表单默认是以get请求提交数据的 http://127.0.0.1:8000/login/?username=admin&password=123 action 1 不写,默认向当前地 ...

  10. 1_03_MSSQL课程_约束详解

    复习: ->创建数据库的SQL:******** create database 数据库名 on ( name=‘’, size=5MB, filegrowth=2MB, filename=&q ...