预备知识:

1.求0—n个数全排列的算法:

 void print_permutation(int n,int *A,int cur){
if(cur==n){
for(int i=;i<cur;i++) cout<<A[i]<<" ";
cout<<endl;
}
else for(int i=;i<=n;i++){
int ok=;
for(int j=;ok&&j<cur;j++)
if(A[j]==i)
ok=;
if(ok){ A[cur]=i;
print_permutation(n,A,cur+);
}
}
}

2.另一种求数组A全排列的方法是使用头文件<algorithm>中的next_permutation()函数,使用之前要排序。

用法如下:

sort(p,p+n);
do{
for(int i=0;i<n;i++)cout<<p[i]<<endl;
}while(next_permutation(p,p+n));

下面进入正题:

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'.
 
解题思路:
  一道常规的全排列问题,解决思路就是对每个单词进行全排列,然后按设定的优先顺序排序后输出。
注意:
1.“For each word in the input”这句话说明对每一个给定的单词都进行全排列输出,并指明了输出的次序由输入次序决定。
2.注意比较函数的严密性
 
代码参考如下:
 #include <iostream>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <time.h>
using namespace std;
#define clock__ (double(clock())/CLOCKS_PER_SEC) const int maxs=;
struct word{
char p[maxs+];
}; bool cmp_letter(const char& a,const char& b){
if((tolower(a)<tolower(b))||(tolower(a)==tolower(b)&&isupper(a)&&islower(b))) return true;
return false;
}
bool cmp_word(const word &a,const word &b){
int len1=strlen(a.p);
int len2=strlen(b.p);
if(len1<len2) return true;
if(len1==len2){
for(int i=;i<len1;i++)
if(cmp_letter(a.p[i], b.p[i]))
return true;
else if(cmp_letter(b.p[i], a.p[i]))
return false ;
}
return false;
}
int n; word p;
int main() {
scanf("%d",&n);
while(n--){
vector<word> q;
scanf("%s",p.p);
int len=strlen(p.p);
sort(p.p, p.p+len);
do{
q.push_back(p);
}while(next_permutation(p.p, p.p+len));
sort(q.begin(), q.end(),cmp_word); for(int i=;i<q.size();i++)
printf("%s\n",q[i].p);
} //printf("time : %lf\n",clock__);
return ;
}

Anagram——[枚举全排列]的更多相关文章

  1. 洛谷 P1120 小木棍 [数据加强版]解题报告

    P1120 小木棍 [数据加强版] 题目描述 乔治有一些同样长的小木棍,他把这些木棍随意砍成几段,直到每段的长都不超过50. 现在,他想把小木棍拼接成原来的样子,但是却忘记了自己开始时有多少根木棍和它 ...

  2. [saiku] 源码整合[maven整合]

    saiku源码的整合分为[普通web项目整合]和[maven整合]两种 本节主要是讲解如何整合为maven项目 转载自:http://blog.csdn.net/gsying1474/article/ ...

  3. [terry笔记]Oracle会话追踪(二):TKPROF

    接上一笔记[terry笔记]Oracle会话追踪(一):SQL_TRACE&EVENT 10046 http://www.cnblogs.com/kkterry/p/3279282.html ...

  4. [terry笔记]Oracle会话追踪(一):SQL_TRACE&EVENT 10046

      SQL_TRACE/10046 事件是 Oracle 提供的用于进行 SQL 跟踪的手段,在日常的数据库问题诊断和解决中是非常常用的方法.但其生成的trace文件需要tkprof工具生成一个可供人 ...

  5. [ Bubble Sort ]& block

    [ Bubble Sort ] 冒泡排序!“预处理.block.预编译”!<环境:Terminal的gcc编译器> 简述:冒泡排序就是把小的元素往前调或者把大的元素往后调.比较是相邻的两个 ...

  6. 洛谷——P1120 小木棍 [数据加强版]

    P1120 小木棍 [数据加强版] 题目描述 乔治有一些同样长的小木棍,他把这些木棍随意砍成几段,直到每段的长都不超过5050. 现在,他想把小木棍拼接成原来的样子,但是却忘记了自己开始时有多少根木棍 ...

  7. 洛谷 P1120 小木棍 [数据加强版]

    P1120 小木棍 [数据加强版] 题目描述 乔治有一些同样长的小木棍,他把这些木棍随意砍成几段,直到每段的长都不超过50. 现在,他想把小木棍拼接成原来的样子,但是却忘记了自己开始时有多少根木棍和它 ...

  8. Can you find it?——[二分查找]

    Description Give you three sequences of numbers A, B, C, then we give you a number X. Now you need t ...

  9. poj3187-Backward Digit Sums(枚举全排列)

    一,题意: 输入n,sum,求1~n的数,如何排列之后,相邻两列相加,直到得出最后的结果等于sum,输出1~n的排列(杨辉三角)  3 1 2 4 //1~n 全排列中的一个排列  4 3 6  7 ...

随机推荐

  1. php实现希尔排序

    对于排序的算法我想大家首先想到的事  冒泡排序:快速排序:或者想起选择和插入排序: 今天的讲解并不是以上四种:而是希尔排序: 对18W个数字排序,时间比较(毫秒) 希尔排序 0.1s 就完成了,有点不 ...

  2. SPSS统计基础-均值功能的使用

    SPSS统计基础-均值功能的使用 均值过程计算一个或多个自变量类别中因变量的子组均值和相关的单变量统计.您也可以获得单因素方差分析.eta 和线性相关检验. 统计量.合计.个案数.均值.中位数.组内中 ...

  3. 将数组对象转换成DataSet

    public static DataSet ObjectArrayToDataSet(object[] objArr) { if (objArr.Length == 0) return null; D ...

  4. Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第十四章:曲面细分阶段

    原文:Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第十四章:曲面细分阶段 代码工程地址: https://github. ...

  5. DRP 2016-06-30 16:36 314人阅读 评论(21) 收藏

    学习drp有一段时间了,其实从很久以前,再提高班的学习就已经不是单纯的学习,学习总是伴随着项目.这就使得我们的学习不可能全天的,大把大把时间的学习只出现在第一和第二年,所以,各自珍惜吧. DRP(Di ...

  6. VirtualBox使用随笔

    1.virtualbox配置Android手机USB热点 host:Windows10;guest:windows XP/10 右击我的电脑 - 管理 - 设备管理器 - 网卡适配器,若手机正确vb连 ...

  7. 【NS2】常用资源(转载)

    (一). NS常用基本网站 1. 寻求问题答案最好的地方.    http://mailman.isi.edu/pipermail/ns-users/ 2. 柯老师的网站,包含很多非常实用资源:安装, ...

  8. Flask学习之九 分页

    英文博客地址:http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-ix-pagination 中文翻译地址:http:// ...

  9. js中setInterval与setTimeout用法 实现实时刷新每秒刷新

    setTimeout 定义和用法:  setTimeout()方法用于在指定的毫秒数后调用函数或计算表达式.     语法:  setTimeout(code,millisec)     参数:    ...

  10. 06Redis入门指南笔记(安全、通信协议、管理工具)

    一:安全 1:可信的环境 Redis以简洁为美.在安全层面Redis也没有做太多的工作.Redis的安全设计是在"Redis运行在可信环境"这个前提下做出的.在生产环境运行时不能允 ...