HDU-#1027 Ignatius and the Princess II(全排列+STL)
来源:互联网 发布:苹果电脑mac玩英雄联盟 编辑:程序博客网 时间:2024/05/18 00:46
题目大意:给出n和m。对1到n进行全排列,按照字典序输出第m个排列的序列。
解题思路:开始以为就是一个全排的题,直接就dfs递归写了个,提交TLE,才发现这样会T。然后查了下,发现这个题有好几种写法,最简单易用的是STL的next_permutation,直接一下就A了。想想STL之所以这么快肯定有什么不一样的实现方法,而递归的写法也可以进行改进,现在主要是没时间,因此,将两个code都贴出来,后边再来完善。详见code。
题目来源:http://acm.hdu.edu.cn/showproblem.php?pid=1027
STL code:
#include <iostream>#include <cstdio>#include <algorithm>using namespace std;const int MAXN = 10000;int n,m;int num[MAXN];int main(){ while(scanf("%d%d",&n,&m)!=EOF){ for(int i=0;i<n;i++) num[i]=i+1; while(--m) //获取第m次排列 next_permutation(num,num+n); for(int i=0;i<n-1;i++) printf("%d ",num[i]); printf("%d\n",num[n-1]); } return 0;}
dfs+递归 code(TLE,待修改):
#include <iostream>#include <cstdio>#include <cstring>using namespace std;const int MAXN = 10000;int n,m,tmp;int rcd[MAXN],num[MAXN],used[MAXN];void full_permutation(int l){ if(l==n){ tmp++; if(tmp==m){ for(int i=0;i<n;i++){ printf("%d",rcd[i]); if(i<n) printf(" "); } printf("\n"); return ; } } for(int i=0;i<n;i++) if(!used[i]){ used[i]=1; rcd[l]=num[i]; full_permutation(l+1); used[i]=0; }}int main(){ while(scanf("%d%d",&n,&m)!=EOF){ memset(used,0,sizeof(used)); for(int i=0;i<n;i++) num[i]=i+1; tmp=0; full_permutation(0); } return 0;}
0 0
- HDU 1027 Ignatius and the Princess II (STL全排列)
- hdu 1027 Ignatius and the Princess II (STL 全排列)
- hdu 1027 Ignatius and the Princess II(STL全排列)
- 【HDU 1027 Ignatius and the Princess II】+ 全排列 + STL
- HDU 1027 Ignatius and the Princess II(全排列)
- HDU-#1027 Ignatius and the Princess II(全排列+STL)
- hdoj 1027 Ignatius and the Princess II 【全排列】【STL】
- HDU 1027 Ignatius and the Princess II 全排列
- hdu 1027 Ignatius and the Princess II 全排列
- HDU 1027 Ignatius and the Princess II 全排列
- hdu 1027 Ignatius and the Princess II (全排列)
- 【杭电oj】1027 - Ignatius and the Princess II(STL - 全排列)
- HDU-1027(Ignatius and the Princess II)(全排列dfs())
- HDU.1027 Ignatius and the Princess II【全排列:关于next_permutation问题】(3.14)
- HDU 1027 Ignatius and the Princess II(stl,next_permutation)
- hdu1027 Ignatius and the Princess II (全排列 & STL中的神器)
- HDOJ 1027Ignatius and the Princess II(全排列)
- HDOJ 1027-Ignatius and the Princess II【全排列】
- python 的日志logging模块学习
- 网络层相关概念
- 2015年华为提前招聘机试题--输出最大的三位数
- CSS自定义字体(@font-face选择符)
- Android输入子系统浅析(一)
- HDU-#1027 Ignatius and the Princess II(全排列+STL)
- 静态路由,缺省路由和默认网关的区别
- POJ—2083—Fractal—【递归与分治】
- AOP中的实体
- 原始的解释器模式(Interpreter Pattern)
- poj 1456 贪心+并查集优化(卖带截止时间货物的最大利润)
- [LeetCode] Construct Binary Tree from Inorder and Postorder Traversal
- ARM-linux驱动学习:led驱动程序编写练习(2014-8-22)
- CCNU_2014_ACM暑假集训总结