排列组合函数next_permutation()
next_permution(),按照字典序进行排列组合,
括号里的参数为类似sort里面的参数,用法相同
#include <bits/stdc++.h>
using namespace std;
#define Maxn 10
int main(){
int a[3];
a[0]=1;a[1]=2;a[2]=3;
do{
cout<<a[0]<<" "<<a[1]<<" "<<a[2]<<endl;
}while (next_permutation(a,a+3)); //参数3指的是要进行排列的长度
}//如果存在a之后的排列,就返回true。如果a是最后一个排列没有后继,返回false,每执行一次,a就变成它的后继

如果交换a[0],a[1],a[2]的大小,排列的次数会改变
#include <bits/stdc++.h>
using namespace std;
#define Maxn 10
int main(){
int a[3];
a[0]=3;a[1]=2;a[2]=1;
do{
cout<<a[0]<<" "<<a[1]<<" "<<a[2]<<endl;
}while (next_permutation(a,a+3)); //参数3指的是要进行排列的长度
}

例题(白书)p78,字母重排
输入一个字典(******结尾),然后输入若干单词w,你都需要在字典中找出所有可以用w的字母重排后得到的单词,并按照字典序从小到大的顺序在一行中输出(
不存在输出:( )输入单词之间用空格或空行隔开,输入单词都不超过6个小写字母组成:
样例输入:
trap given score refund only trap work earn course pepper part
******
resco nfudre aptr sett oresuc
输出
score
refund
part trap trap
:(
course
#include <bits/stdc++.h>
using namespace std;
#define Maxn 10
char MAP[Maxn][Maxn];
char AIM[Maxn][Maxn];
int main(){
string A;
string B;
string C;
getline(cin,A);
getline(cin,B);
getchar();
getline(cin,C);
int j = 0;
int k = 0;
for( int i = 0; i < A.length(); i++ ){
if(A[i] == ' '){
MAP[k][j] = '\0';
k++;
j = 0;
}else{
MAP[k][j] = A[i];
j++;
}
}
MAP[k++][j] = '\0';
set<string>s;
for(int i = 0; i < k; i++){
s.insert(MAP[i]);
}
int y = 0;
int t = 0;
for( int i = 0; i < C.length(); i++ ){
if(C[i] == ' '){
AIM[y][t++] = '\0';
y++;
t = 0;
}else{
AIM[y][t] = C[i];
t++;
}
}
AIM[y++][t] = '\0';
int cnt = 0;
for(int i = 0; i < y; i++){
sort(AIM[i],AIM[i] + strlen(AIM[i]));
bool flag = true;
do{
for(int j = 0; j < k; j++){
if(strcmp(AIM[i],MAP[j]) == 0){
flag = false;
cout << AIM[i] << " ";
}
}
}while( next_permutation(AIM[i],AIM[i] + strlen(AIM[i]) ) );//全排列
if(flag){
cout << ":(";
}
cout << endl;
}
}
排列组合函数next_permutation()的更多相关文章
- python自带的排列组合函数
需求: 在你的面前有一个n阶的台阶,你一步只能上1级或者2级,请计算出你可以采用多少种不同的方法爬完这个楼梯?输入一个正整数表示这个台阶的级数,输出一个正整数表示有多少种方法爬完这个楼梯. 分析:提炼 ...
- python内置函数-排列组合函数
product 笛卡尔积 (有放回抽样排列) permutations 排列 (不放回抽样排列) combinations 组合,没有重复 (不放回抽样组合) combinations_with_re ...
- php 排列组合函数(无重复组合,可重复组合【全排列组合】)
<?php /** * 无重复排列組合 * @Author MAX * @DateTime 2018-09-07T16:28:40+0800 * @param Array $arr 需要排列組合 ...
- R语言学习笔记:choose、factorial、combn排列组合函数
一.总结 组合数:choose(n,k) —— 从n个中选出k个 阶乘:factorial(k) —— k! 排列数:choose(n,k) * factorial(k) 幂:^ 余数:%% 整数商: ...
- HDOJ 1716 排列2(next_permutation函数)
Problem Description Ray又对数字的列产生了兴趣: 现有四张卡片,用这四张卡片能排列出很多不同的4位数,要求按从小到大的顺序输出这些4位数. Input 每组数据占一行,代表四张卡 ...
- nyoj19(排列组合next_permutation(s.begin(),s.end()))
题目意思: 从n个数中选择m个数,按字典序输出其排列. pid=19">http://acm.nyist.net/JudgeOnline/problem.php?pid=19 例: 输 ...
- C++ STL next_permutation(快速排列组合)
排列组合必备!! https://blog.csdn.net/bengshakalakaka/article/details/78515480
- ACM~排列组合&&hdu例子
排列组合是数学中的一个分支.在计算机编程方面也有非常多的应用,主要有排列公式和组合公式.错排公式.母函数.Catalan Number(卡特兰数)等. 一.有关组合数学的公式 1.排列公式 P(n ...
- C++全排列函数next_permutation()和prev_permutation()
头文件:#include<algorithm> * * * 1. next_permutation(): next_permutation()函数的返回类型是bool类型. 即:如果有一个 ...
随机推荐
- SqlServer日期(convert函数,getdate函数)
SqlServer日期(convert函数,getdate函数) 函数GETDATE()的返回值在显示时只显示到秒.实际上,SQL Sever内部时间可以精确到毫秒级(确切地说,可以精确到3.33毫秒 ...
- cmake 安装 mysql
因为高版本mysql都用cmake安装,另外安装cluster版的mysql也必须通过cmake安装,所以学习cmake安装mysql很有必要. 今天我因为打算搭配一个mysql集群所以,在虚拟机上安 ...
- sql性能优化总结(转)
网上看到一篇sql优化的文章,整理了一下,发现很不错,虽然知道其中的部分,但是没有这么全面的总结分析过…… 一. 目的 数据库参数进行优化所获得的性能提升全部加起来只占数据库应用系统性能提升的40 ...
- Razor引擎总结
1.显示格式化小数:@(string.Format("{0:0.00}",ViewData["TradeAmount"].ToNullString()))
- aspose.cells根据模板导出excel
又隔十多天没写博客了,最近都在忙项目的事情,公司人事变动也比较大,手头上就又多了一个项目.最近做用aspose.cells根据模板导出excel报价单的功能,顺便把相关的核心记下来,先上模板和导出的效 ...
- Servlet+Tomcat制作出第一个运行在Tomcat上的Java应用程序
转载自:http://www.linuxidc.com/Linux/2011-08/41685.htm [日期:2011-08-27] 来源:csdn 作者:Cloudyxuq 1.IDE工 ...
- 解决方案:安装wordpress出现500 Internal Server Error
做一个资讯站点的时候遇到一个wordpress不知道算不算常见的问题:程序安装的时候提示500 Internal Server Error 那么最终百度谷歌找到以下解决方案: 安装新版本wordpre ...
- Security 可视化
http://web.cs.ucdavis.edu/~ma/SecVis/ http://research.dbvis.de/security/tools/clockview/ http://raff ...
- 【POJ2406】 Power Strings (KMP)
Power Strings Description Given two strings a and b we define a*b to be their concatenation. For exa ...
- HTML5的Server-Sent Events功能的使用
客户端代码示例 //创建一个新的 EventSource 对象,然后规定发送更新的页面的 URL. var source = new EventSource("http://localhos ...