UVA 10098 用字典序思想生成所有排列组合
题目:
Generating permutation has always been an important problem in computer science. In this problem
you will have to generate the permutation of a given string in ascending order. Remember that your
algorithm must be efficient.
Input
The rst line of the input contains an integer n, which indicates how many strings to follow. The next
n lines contain n strings. Strings will only contain alpha numerals and never contain any space. The
maximum length of the string is 10.
Output
For each input string print all the permutations possible in ascending order. Not that the strings should
be treated, as case sensitive strings and no permutation should be repeated. A blank line should follow
each output set.
Sample Input
3
ab
abc
bca
Sample Output
ab
ba
abc
acb
bac
bca
cab
cba
abc
acb
bac
bca
cab
cba
题意:
输入字符串个数n,接下来依次输入n个字符串,要求按字典序从小到大输出每一个字符串的所有排列,不同字符串的全排列之间有一个空行。
分析:我在上一篇博客中写到了如何生成该字符串按字典序排列的下一个字符串--【https://www.cnblogs.com/cautx/p/11403927.html】
那么如何利用这个按字典序生成下一个排列的算法来生成全排列?
首先将该字符串按字典序从小到大排列:sort(s,s+len);
然后利用这个算法不断生成下一个排列并输出即可,只需要修改一下原算法的循环条件即可。
AC code1:
#include<bits/stdc++.h>
using namespace std;
char s[];
int len;
int solve()
{
int id=len-;
while(id>=)
{
if(s[id-]<s[id]) break;
else id--;
}
if(id==) return ;
int mpre=id-,mnow=id;
for(int j=mnow+;j<len;j++)
{
if(s[j]<=s[mpre]) continue;
if(s[j]<s[mnow]) mnow=j;
}
swap(s[mnow],s[mpre]);
sort(s+id,s+len);
return ;
}
int main()
{
//freopen("input.txt","r",stdin);
int n;
scanf("%d",&n);
while(n--)
{
scanf("%s",s);
len=strlen(s);
sort(s,s+len);
printf("%s\n",s);
while(solve()) printf("%s\n",s);
printf("\n");
}
return ;
}
实际上,C++的STL的next_permutation(s,s+len)就是按照字典序来实现这个算法的。
AC code2:
#include<bits/stdc++.h>
using namespace std;
char s[];
int main()
{
//freopen("input.txt","r",stdin);
int n;
scanf("%d",&n);
while(n--)
{
scanf("%s",s);
int len=strlen(s);
sort(s,s+len);
do{
printf("%s\n",s);
}while(next_permutation(s,s+len));
printf("\n");
}
return ;
}
UVA 10098 用字典序思想生成所有排列组合的更多相关文章
- POJ 1146 ID Codes 用字典序思想生成下一个排列组合
ID Codes Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7644 Accepted: 4509 Descript ...
- [Bzoj3193][JLOI2013]地形生成 (排列组合 + DP)
3193: [JLOI2013]地形生成 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 459 Solved: 223[Submit][Status ...
- [bzoj3193][JLOI2013]地形生成_排列组合_贪心
[JLOI2013]地形生成 题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=3193 题解: 这种求总排列的题,一种常规做法就是所有的元素 ...
- uva 11174 Stand in a Line (排列组合)
UVa Online Judge 训练指南的题目. 题意是,给出n个人,以及一些关系,要求对这n个人构成一个排列,其中父亲必须排在儿子的前面.问一共有多少种方式. 做法是,对于每一个父节点,将它的儿子 ...
- Day2:T3DP(基于排列组合思想)
T3:DP(基于排列组合思想的状态转移) 其实之前写排列组合的题目有一种很茫然的感觉.... 应该是因为之前没有刷过所以没有什么体会 上次刷的vj1060有用到,但是写状态转移还是第一次学习吧 ccy ...
- 【原创】开源.NET排列组合组件KwCombinatorics使用(二)——排列生成
本博客所有文章分类的总目录:本博客博文总目录-实时更新 本博客其他.NET开源项目文章目录:[目录]本博客其他.NET开源项目文章目录 KwCombinatorics组件文章目录: 1. ...
- 【原创】开源.NET排列组合组件KwCombinatorics使用(一)—组合生成
本博客所有文章分类的总目录:本博客博文总目录-实时更新 本博客其他.NET开源项目文章目录:[目录]本博客其他.NET开源项目文章目录 KwCombinatorics组件文章目录: 1. ...
- 给定数组a[1,2,3],用a里面的元素来生成一个长度为5的数组,打印出其排列组合
给定数组a[1,2,3],用a里面的元素来生成一个长度为5的数组,打印出其排列组合 ruby代码: def all_possible_arr arr, length = 5 ret = [] leng ...
- UVa Problem 10132 File Fragmentation (文件还原) 排列组合+暴力
题目说每个相同文件(01串)都被撕裂成两部分,要求拼凑成原来的样子,如果有多种可能输出一种. 我标题写着排列组合,其实不是什么高深的数学题,只要把最长的那几个和最短的那几个凑一起,然后去用其他几个验证 ...
随机推荐
- 个人永久性免费-Excel催化剂功能第61波-快速锁定解锁单元格及显示隐藏公式
Excel的所有功能都是需求导向的,正因为有客户在企业管理的过程中,有这样的需求出现了,然后相应的Excel就出现了相应的功能来辅助管理,学习Excel的功能,其实真的可以学习到先进企业的许多的管理思 ...
- Excel催化剂开源第7波-VSTO开发中Ribbon动态加载菜单
在VS开发环境中,特别是VSTO的开发,微软已经现成地给开发者准备了设计器模式的功能区开发,相对传统的VBA.ExcelDna和其他方式的COM加载项开发来说,不需要手写xml功能区,直接类似拖拉窗体 ...
- AndroidStudio使用genymotion模拟器
安装Genymotion之前首先要安装好virtualbox这个软件 virtual官方网站:https://www.virtualbox.org/ genymotion的官方网站: https:// ...
- 如何实现Kali linux系统下的U盘启动(小白指导)
一.准备工作: 声明:这个“操作”并不会影响你原装的系统,真正的即插即用的哦. (1)4GB的U盘<读写速度比较快的> (2)Kali linux镜像文件 (3)软件Universal-U ...
- c++容器简单总结
数据结构 描述 实现头文件 向量(vector) 连续存储的元素 <vector> 列表(list) 由节点组成的双向链表,每个结点包含着一个元素 <list> 双队列(deq ...
- datatables editor fields type
其实editor fields type 默认支持的输入类型就是w3c输入框类型. text number password textarea select checkbox ...
- PHP与ECMAScript_5_常用数组相关函数
PHP ECMAScript 长度 $length = count($array) length = array.length 增 array_unshift($array, new1,n ...
- 百度网盘 人工智能书籍【Tensorflow和深度学习】
链接:https://pan.baidu.com/s/1ejCvwn08ILI2fMhBEdXR8w 提取码:6pk9
- CentOS7使用yum安装ceph rpm包
1. 安装centos7对扩展repo的支持yum install yum-plugin-priorities保证下面的选项是开启的[main]enabled = 1 2. 安装 release.ke ...
- Android buildType混淆代码
[话题引入] ①在Android开发完成,我们会将代码打包成APK文件.选择 菜单栏 Build --> Build APK ②将查看视图切换到 Project 模式,文件夹下有一个debug模 ...