递归解决全排列
来源:互联网 发布:角谷猜想知乎 编辑:程序博客网 时间:2024/06/11 05:42
A permutation is a possible ordering of a set. For example, the permutations of a set {A,B,C} include ABC, ACB, BAC, BCA, CAB, CBA.
The number of permutations of a set of N elements is N!. The example set above has 3 elements and it has a number of 3!=6 permutations.
Given a line with N different characters, please print N! lines containing all different permutations of the N characters. These N! lines are printed in dictionary order.
A line with N different characters。(1<=N<=8)
N! lines containing all different permutations of the N characters. These N! lines are printed in dictionary order.
ABCD
ABCDABDCACBDACDBADBCADCBBACDBADCBCADBCDABDACBDCACABDCADBCBADCBDACDABCDBADABCDACBDBACDBCADCABDCBA
这道题要求输出字母的全排列,可以运用递归解决。
下面是代码:
#include<bits/stdc++.h>
#include<string.h>
using namespace std;
void premutation(string pre,string s)
{
if(s=="") cout<<pre<<endl;
for(int i=0;i<s.size();i++)
{
string str=s;
premutation(pre+s[i],str.erase(i,1));
}
}
int main()
{
string s,pre="";
cin>>s;
premutation(pre,s);
return 0;
}
这道题用到了erase函数,我们可以看一下它的用法:
string s;
1. s.erase(2.3) 删除第二个字符后(不包括)的三个字符,即删除s[2],s[3],s[3]。eg. 12345 ,删除3.4.5,剩下1.2.
2. s.erase(s.begin()+2,s.end()-1) 删除之间的字符,是不是可以这样理解,s.begin()==s[0],s.begin()+2==s[2].同理,在下例中,s.end()-1==s[3]。eg.12345,删除34,输出125.
3. s.erase(posion) .......(这个以后来补充。。。。。)
- 递归解决全排列
- 递归解决全排列问题
- 递归解决全排列问题
- 递归分治解决全排列问题
- 递归解决全排列生成算法
- java使用递归解决全排列问题
- 递归解决全排列生成算法
- 递归解决全排列生成算法
- 递归解决全排列生成算法
- 递归解决全排列和幻方
- 利用递归解决全排列问题
- Java递归解决全排列问题
- 递归解决全排列问题+详细图解递归执行
- 递归--全排列
- 全排列递归算法
- 递归 全排列 问题
- 全排列递归算法
- 全排列算法-递归
- GDI+ 双缓冲实现和局部刷新技术
- 高斯曲线拟合原理及实现
- ViewController生命周期
- Jsoup 网页抓取数据之获取汽车时刻表 实例解析
- UVA146 ID codes
- 递归解决全排列
- spring boot jsp
- funm--矩阵的函数运算
- Linux下Socket编程
- Cookie,Session验证机制
- 数据结构课程设计之2048闯关版
- 《暗黑王座》10日双平台公测 三年夜女神代言(视频)
- Android从一个URL下载原始数据,基于byte字节
- mysql case when