全排列 next_permutation() 函数的使用
看来看去还是这篇博客比较简洁明了
https://www.cnblogs.com/My-Sunshine/p/4985366.html
顺便给出牛客网的一道题,虽然这道题用dfs写出全排列也能做,题意小心理解,后面给出题目和别人AC代码吧,手动心累。
https://www.nowcoder.com/acm/contest/156/D
这个函数包含在头文件 include <algorithm> 里面
基本格式
do
{ ...
}while(next_permutation(a, a + n));
#include <iostream>
#include <algorithm>
using namespace std;
const int MAXN = ;
int main()
{
int n;
while(cin >> n && n)
{
int a[MAXN];
for(int i = ;i < n;++i)
cin >> a[i];
// 因为这个 next_permutation() 是按字典序排序结束的,所以要先 sort 排序
// 自己可以试试把 sort 去掉会有什么结果
// 返回值据说是 bool 类型
sort(a, a + n);
do
{
for(int i = ;i < n;++i)
cout << a[i] << " ";
cout << endl;
}while(next_permutation(a, a + n));
}
return ;
}
输入
3
3 2 1
输出
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
****************************************
把 sort 去掉后
输入
3
3 2 1
输出
3 2 1
******************************************
链接:https://www.nowcoder.com/acm/contest/156/D
来源:牛客网
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld
题目描述
而咒语的总伤害为所有 'a'... 'i' 的排列造成的伤害值之和,托米能打出多少点的伤害,是否能击败 1317 呢?
输入描述:
一行输入一个字符串 s
输出描述:
一行输出一个数,表示伤害值
输入例子:
aabcdefghi
输出例子:
1
-->
备注:
|s| ≤ 3000
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; char str[]; int main()
{
char ss[]={"abcdefghi"};
scanf("%s",str);
register int ans=;
do{
register int i=,j=;
while(i<&&str[j])
{
if(ss[i]==str[j]){
i++,j++;
}else{
j++;
}
}
if(i==) ans++;
}while(next_permutation(ss,ss+));
printf("%d\n",ans);
return ;
}
全排列 next_permutation() 函数的使用的更多相关文章
- 关于全排列 next_permutation() 函数的用法
这是一个c++函数,包含在头文件<algorithm>里面,下面是基本格式. 1 int a[]; 2 do{ 3 4 }while(next_permutation(a,a+n)); 下 ...
- 全排列 next_permutation() 函数的用法
在头文件<algorithm>里面有如下代码: int a[]; do { } while(next_permutation(a,a+n)); 可产生1~n的全排列有如下代码: #incl ...
- C++中全排列算法函数next_permutation的使用方法
首先,先看对next_permutation函数的解释: http://www.cplusplus.com/reference/algorithm/next_permutation/?kw=next_ ...
- 全排列函数 nyoj 366(next_permutation()函数)
C++ STL中提供了std::next_permutation与std::prev_permutation可以获取数字或者是字符的全排列,其中std::next_permutation提供升序.st ...
- HDOJ 1716 排列2 next_permutation函数
Problem Description Ray又对数字的列产生了兴趣: 现有四张卡片,用这四张卡片能排列出很多不同的4位数,要求按从小到大的顺序输出这些4位数. Input 每组数据占一行,代表四张卡 ...
- POJ-1256 next_permutation函数应用
字典序列: 在字典序中蕴含着一个点,就是大小的问题,谁先出现,谁后出现的问题.譬如a<b<c,出现顺序就是a,b,c. 本题中字符集是所有大小写字母,而题目中规定的谁大谁小已经不是按asc ...
- c++中STL中的next_permutation函数基本用法
对于next_permutation函数是针对于排列组合问题的库函数,它的排序方式是按照字典的方式排列的·: 如以下代码对于next_permutation函数的初步解释: #include<c ...
- hdu 1027 Ignatius and the Princess II(产生第m大的排列,next_permutation函数)
题意:产生第m大的排列 思路:使用 next_permutation函数(头文件algorithm) #include<iostream> #include<stdio.h> ...
- 全排列next_permutation()用法和构造函数赋值
全排列next_permutation()用法 在头文件aglorithm里 就是1~n数组的现在的字典序到最大的字典序的依次增加.(最多可以是n!种情况) int a[n]; do{ }while( ...
随机推荐
- M(必备),R(需求),C(条件),O(可选)
M:must 必备 R:request 需求 C:condition 条件 O:option 可选 AFL:application file locator 应用文件定位器 PKI:公钥索引 IPK: ...
- 编写高质量代码改善C#程序的157个建议——建议55:利用定制特性减少可序列化的字段
建议55:利用定制特性减少可序列化的字段 特性(attribute)可以声明式地为代码中的目标元素添加注释.运行时可以通过查询这些托管块中的元数据信息,达到改变目标元素运行时行为的目的.System. ...
- linux系统下ipmitool添加BMC帐号密码
需求:已知BMC帐号id2为root管理员帐号,添加id5bmc帐号 工具:ipmitool version 1.8.14 系统:CentOS release 6.6 (Final) 1,通过yum安 ...
- [转]Marshaling a SAFEARRAY of Managed Structures by P/Invoke Part 5.
1. Introduction. 1.1 In part 4, I have started to discuss how to interop marshal a managed array tha ...
- 实现求解线性方程(矩阵、高斯消去法)------c++程序设计原理与实践(进阶篇)
步骤: 其中A是一个n*n的系数方阵 向量x和b分别是未知数和常量向量: 这个系统可能有0个.1个或者无穷多个解,这取决于系数矩阵A和向量b.求解线性系统的方法有很多,这里使用一种经典的方法——高斯消 ...
- Xcode9新功能
1.折叠代码 局部折叠(折叠一个函数):Command+Option+Left/Right 全局折叠(折叠当前文件下的全部函数): Shift+Command+Option+Left/Right 折叠 ...
- vmware实现物理机和虚拟机复制粘贴
要实现物理机和虚拟机的复制粘贴需要安装VMware Tools. 1.点击菜单栏--虚拟机--安装VMware Tools. 2.打开linux终端,进入/media/VMware Tools目录. ...
- svn服务器发生变更,如何切换
参考链接: https://blog.csdn.net/jk110333/article/details/9301283 https://blog.csdn.net/emtit2008/article ...
- java 图书馆系统 练习
话不多说 娱乐 ================================================== book 类(书本的基础属性) package 图书管理系统01; /** * @ ...
- Atcoder Grand Contest 031B(DP,思维)
#include<bits/stdc++.h>using namespace std;int a[200007];int b[200007];long long dp[200007];lo ...