Anagram——[枚举全排列]
预备知识:
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
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
Output
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
So the right order of letters is 'A'<'a'<'B'<'b'<...<'Z'<'z'.
#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——[枚举全排列]的更多相关文章
- 洛谷 P1120 小木棍 [数据加强版]解题报告
P1120 小木棍 [数据加强版] 题目描述 乔治有一些同样长的小木棍,他把这些木棍随意砍成几段,直到每段的长都不超过50. 现在,他想把小木棍拼接成原来的样子,但是却忘记了自己开始时有多少根木棍和它 ...
- [saiku] 源码整合[maven整合]
saiku源码的整合分为[普通web项目整合]和[maven整合]两种 本节主要是讲解如何整合为maven项目 转载自:http://blog.csdn.net/gsying1474/article/ ...
- [terry笔记]Oracle会话追踪(二):TKPROF
接上一笔记[terry笔记]Oracle会话追踪(一):SQL_TRACE&EVENT 10046 http://www.cnblogs.com/kkterry/p/3279282.html ...
- [terry笔记]Oracle会话追踪(一):SQL_TRACE&EVENT 10046
SQL_TRACE/10046 事件是 Oracle 提供的用于进行 SQL 跟踪的手段,在日常的数据库问题诊断和解决中是非常常用的方法.但其生成的trace文件需要tkprof工具生成一个可供人 ...
- [ Bubble Sort ]& block
[ Bubble Sort ] 冒泡排序!“预处理.block.预编译”!<环境:Terminal的gcc编译器> 简述:冒泡排序就是把小的元素往前调或者把大的元素往后调.比较是相邻的两个 ...
- 洛谷——P1120 小木棍 [数据加强版]
P1120 小木棍 [数据加强版] 题目描述 乔治有一些同样长的小木棍,他把这些木棍随意砍成几段,直到每段的长都不超过5050. 现在,他想把小木棍拼接成原来的样子,但是却忘记了自己开始时有多少根木棍 ...
- 洛谷 P1120 小木棍 [数据加强版]
P1120 小木棍 [数据加强版] 题目描述 乔治有一些同样长的小木棍,他把这些木棍随意砍成几段,直到每段的长都不超过50. 现在,他想把小木棍拼接成原来的样子,但是却忘记了自己开始时有多少根木棍和它 ...
- 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 ...
- poj3187-Backward Digit Sums(枚举全排列)
一,题意: 输入n,sum,求1~n的数,如何排列之后,相邻两列相加,直到得出最后的结果等于sum,输出1~n的排列(杨辉三角) 3 1 2 4 //1~n 全排列中的一个排列 4 3 6 7 ...
随机推荐
- 阿里工程师开发了一款免费工具,提升Kubernetes应用开发效率
对于使用了Kubernetes作为应用运行环境的开发者而言,在同一个集群中我们可以使用命名空间(Namespace)快速创建多套隔离环境,在相同命名空间下,服务间使用Service的内部DNS域名进行 ...
- LintCode刷题笔记-- Update Bits
标签: 位运算 描述: Given two 32-bit numbers, N and M, and two bit positions, i and j. Write a method to set ...
- .net WebServer示例及调用(接口WSDL动态调用 JAVA)
新建.asmx页面 using System; using System.Collections.Generic; using System.Linq; using System.Web; using ...
- 【转载】Jmeter之Bean shell使用(二)
Jmeter之Bean shell使用(二) 原博文地址为:https://www.cnblogs.com/puresoul/p/4949889.html 其中需要注意的是——三.自定义函数中Bean ...
- 简单的requestAnimationFrame动画
html部分 <div id="test" style="width:1px;height:17px;background:#0f0;">0%< ...
- 连接池c3p0
连接池c3p0 C3P0:hibernate和spring使用,有自动回收空闲连接的功能. 使用步骤: 1.导入jar包(c3p0-0.9.1.2.jar) 2.使用api a.硬编码(不推荐) ne ...
- (译)Objective-C的动态特性
这是一篇译文,原文在此,上一篇文章就是受这篇文章启发,这次干脆都翻译过来. 过去的几年中涌现了大量的Objective-C开发者.有些是从动态语言转过来的,比如Ruby或Python,有些是从强类型 ...
- 简单利用XSS获取Cookie信息实例演示
简单利用XSS获取Cookie信息实例演示 首先要找到一个有XXS的站,这里就不整什么大站了,谷歌一下inurl:'Product.asp?BigClassName',搜出来的命中率也比较高.随便 ...
- MaxCompute 图计算用户手册(上)
概要 ODPS GRAPH是一套面向迭代的图计算处理框架.图计算作业使用图进行建模,图由点(Vertex)和边(Edge)组成,点和边包含权值(Value),ODPS GRAPH支持下述图编辑操作: ...
- linux下安装使用MySQL 以及 python mysqldb 遇到的问题
一.安装mysql sudo apt-get install mysql-client-core-5.5 然后会出现: ERROR 2002 (HY000): Can't connect to loc ...