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,就使得每一个大写字母刚好穿进了小写字母中。

代码:

  1. #include <iostream>
  2. #include <algorithm>
  3. #include <cmath>
  4. #include <cstring>
  5. #include <string>
  6. using namespace std;
  7. char s[5000];
  8. bool cmp(char a,char b)
  9. {
  10. double front,behind;
  11. if(a>='A'&&a<='Z')
  12. front = (double)a+31.5;
  13. else
  14. front = (double)a;
  15. if(b>='A'&& b<='Z')
  16. behind = (double)b+31.5;
  17. else
  18. behind = (double)b;
  19. return front<behind;
  20. }
  21. int main()
  22. {
  23. int count;
  24. cin>>count;
  25. while(count--)
  26. {
  27. cin>>s;
  28. sort(s,s+strlen(s),cmp);
  29. do
  30. {
  31. cout<<s<<endl;
  32. }while(next_permutation(s,s+strlen(s),cmp));
  33. }
  34. return 0;
  35. }

版权声明:本文为博主原创文章,未经博主允许不得转载。

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. webservice之实现天气预报

    前通过传智的视频自学了webservice的基本使用,也了解到webservice就是一种跨编程语言和跨操作系统平台的远程调用技术. 对于这些理论知识在这里也不再做过多的解释,本次主要就是记录与分享使 ...

  2. Java解析json数组三种情况

    package com.example.demo.json; import java.util.Map; import com.alibaba.fastjson.JSON; import com.al ...

  3. Codeforces1307D. Cow and Fields

    对于本题,最短路,考虑bfs,那么我们可以跑2次bfs,求出每个点到1与n的最短路,设为x_a, x_b,那我们可以把问题转换成max(min{x_a+y_b,x_b+y_a}+1)(x,y属于1到n ...

  4. 更改windows系统的快捷键方法

    众所周知,windows平台有很多快捷键使用非常的别扭. 现在提供windows 平台快捷键替换的绝佳软件:autohotkey 下载链接:http://ahkscript.org/ 中文帮助站点:h ...

  5. 报警视图 报警窗口 报警指示器 的组态 PLC变量 事故信息 MW16 报警确认变量 MW18 转速变量 MW20 温度变量 MW22 用 M17.0 来模拟事故信息的最低位。用PLCSIM 给定温度为800 度 报警视图与报警窗口显示故障

    组态报警并用PLCSIM进行仿真 步骤1 : 组态离散变量报警 在PLC 的默认变量表中 创建变量"事故信息" 数据类型是word 绝对地址是MW16 同时建立 报警确认变量 MW ...

  6. java第二次课件课后动手动脑习题整理总结(2019年9月23号)

    一.动手动脑 1 1.题目 2.程序源代码 package yang8; import java.util.Scanner; import java.util.Random; public class ...

  7. Hibernate一对多(多对一)外键设置汇总

    我打算在角色表(role)中添加一个帐号表(account)的外键(accountId),步骤如下: 1.首先在角色表(role)中添加列. 添加语句:alter table role add(acc ...

  8. MyEclipse插件github安装使用

    MyEclipse插件github安装使用   网络上的介绍一堆堆的,但是自己尝试了下,发现问题很多,就动手做个教程. 大纲 1.git客户端安装 2.ssh配置 3.egit安装配置 4.参考资料 ...

  9. Lamda简单使用

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  10. String和Date 互相转换

    1.String ->Date String StrDate = "2012-12-12"; SimpleDateFormat sdf=new SimpleDateForma ...