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. Linux centosVMware运行告警系统、分发系统-expect讲解、自动远程登录后,执行命令并退出、expect脚本传递参数、expect脚本同步文件、指定host和要同步的文件、shell项目-分发系统-构建文件分发系统、分发系统-命令批量执行

    一运行告警系统 创建一个任务计划crontab -e 每一分钟都执行一次 调试时把主脚本里边log先注释掉 再次执行 没有发现502文件说明执行成功了,每日有错误,本机IP 负载不高 二.分发系统-e ...

  2. P1120/UVA307 小木棍(sticks) 题解

    题目描述 pdf 题解 注意的问题是,各个原始木棒的长度都是一样的! 说一下本题的总思路即:DFS+超强力剪枝!(详见本人的 AC 程序) 首先,我们要从小到大枚举原始木棒的长度len,也就是枚举答案 ...

  3. js图片瀑布流效果

    要实现图片瀑布流效果,首先得准备几张图片. html的部分比较简单就是将图片加载到浏览器就可以了 代码如下(注意放的图片多一点要不然之后无法滑动鼠标就无法达到瀑布流效果): <!DOCTYPE ...

  4. 「CF852D」Exploration Plan

    题目描述 给定一张 \(V\) 个点,\(M\) 条边的边带权无向图,有 \(N\) 个人分布在图上的点上,第 \(i\) 个人在 \(x_i\) 这个点上,定义从一个点走到另一个点的时间为所走的路径 ...

  5. Django 学习之内置Admin

    一.Admin组件 Django内置的Admin是对于model中对应的数据表进行增删改查提供的组件,使用方式有: Django Admin内部依赖: 依赖APP: django.contrib.au ...

  6. pytorch神经网络解决回归问题(非常易懂)

    对于pytorch的深度学习框架,在建立人工神经网络时整体的步骤主要有以下四步: 1.载入原始数据 2.构建具体神经网络 3.进行数据的训练 4.数据测试和验证 pytorch神经网络的数据载入,以M ...

  7. PDO 小知识

    一.前言 PDO(PHP Data Object)提供了一个通用接口访问多种数据库,即抽象的数据模型支持连接多种数据库. PDO扩展为PHP定义了一个访问数据库的轻量.持久的接口.其本身并不能实现任何 ...

  8. python学习第二课——while循环

    #while循环基础语句 while 1==1: print('OK') #死循环 #如何阻止死循环 count=0 while count<10: print('第'+(str)(count) ...

  9. 如何解决ajax跨域请求?

    1.什么是跨域? a.域名不同 b.域名相同,端口不同 注:只有域名相同,端口相同时,才可以访问数据 可以使用jsonp解决ajax跨域请求的问题. 2.什么是jsonp? Jsonp其实就是一个跨域 ...

  10. 常见加密解密 -- pycryptodomex库

    安装 windows pip install pycryptodomex ubuntu pip install pycryptodome 加密方式 单向加密:MD5 只能对数据进行加密,而不能解密 对 ...