buaaoj230——next_permutation的应用
简单的全排列输出,借用stl中的next_permutation就非常简单了。
关于next_permutation:(备忘,来源网络)
/*这是一个求一个排序的下一个排列的函数,可以遍历全排列,要包含头文件<algorithm>
与之完全相反的函数还有prev_permutation*/ //(1) int 类型的next_permutation int main()
{
int a[];
a[]=;a[]=;a[]=;
do
{
cout<<a[]<<" "<<a[]<<" "<<a[]<<endl;
} while (next_permutation(a,a+)); //参数3指的是要进行排列的长度 //如果存在a之后的排列,就返回true。如果a是最后一个排列没有后继,返回false,每执行一次,a就变成它的后继 } 输出: 如果改成 while(next_permutation(a,a+));
则输出: 只对前两个元素进行字典排序
显然,如果改成 while(next_permutation(a,a+)); 则只输出: 若排列本来就是最大的了没有后继,则next_permutation执行后,会对排列进行字典升序排序,相当于循环 int list[]={,,};
next_permutation(list,list+);
cout<<list[]<<" "<<list[]<<" "<<list[]<<endl; //输出: 1 2 3 () char 类型的next_permutation int main()
{
char ch[];
cin >> ch; sort(ch, ch + strlen(ch) );
//该语句对输入的数组进行字典升序排序。如输入9874563102 cout<<ch; 将输出0123456789,这样就能输出全排列了 char *first = ch;
char *last = ch + strlen(ch); do {
cout<< ch << endl;
}while(next_permutation(first, last));
return ;
} //这样就不必事先知道ch的大小了,是把整个ch字符串全都进行排序
//若采用 while(next_permutation(ch,ch+5)); 如果只输入1562,就会产生错误,因为ch中第五个元素指向未知
//若要整个字符串进行排序,参数5指的是数组的长度,不含结束符 () string 类型的next_permutation int main()
{
string line;
while(cin>>line&&line!="#")
{
if(next_permutation(line.begin(),line.end())) //从当前输入位置开始
cout<<line<<endl;
else cout<<"Nosuccesor\n";
}
} int main()
{
string line;
while(cin>>line&&line!="#")
{
sort(line.begin(),line.end());//全排列
cout<<line<<endl;
while(next_permutation(line.begin(),line.end()))
cout<<line<<endl;
}
} next_permutation 自定义比较函数 #include<iostream> //poj 1256 Anagram
#include<string>
#include<algorithm>
using namespace std;
int cmp(char a,char b) //'A'<'a'<'B'<'b'<...<'Z'<'z'.
{
if(tolower(a)!=tolower(b))
return tolower(a)<tolower(b);
else
return a<b;
}
int main()
{
char ch[];
int n;
cin>>n;
while(n--)
{
scanf("%s",ch);
sort(ch,ch+strlen(ch),cmp);
do
{
printf("%s\n",ch);
}while(next_permutation(ch,ch+strlen(ch),cmp));
}
return ;
}
本题参考代码:
#include <cstdio>
#include <algorithm>
using namespace std;
int main()
{
int a[],l,i;
while(scanf("%d",&l)&&l!=)
{
for(i=;i<;i++)
a[i]=i+;
do{
for(i=;i<l;i++)
printf("%d",a[i]);
printf("\n");
}while (next_permutation(a, a + l));
printf("\n");
}
}
/*这是一个求一个排序的下一个排列的函数,可以遍历全排列,要包含头文件<algorithm>与之完全相反的函数还有prev_permutation*/    //(1) int 类型的next_permutation  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就变成它的后继    }  输出:   1 2 3 1 3 2 2 1 3 2 3 1 3 1 2 3 2 1    如果改成 while(next_permutation(a,a+2));则输出: 1 2 3 2 1 3  只对前两个元素进行字典排序显然,如果改成 while(next_permutation(a,a+1)); 则只输出:1 2 3      若排列本来就是最大的了没有后继,则next_permutation执行后,会对排列进行字典升序排序,相当于循环   int list[3]={3,2,1};next_permutation(list,list+3);cout<<list[0]<<" "<<list[1]<<" "<<list[2]<<endl;  //输出: 1 2 3        (2) char 类型的next_permutation  int main(){ char ch[205];cin >> ch;  sort(ch, ch + strlen(ch) );//该语句对输入的数组进行字典升序排序。如输入9874563102 cout<<ch; 将输出0123456789,这样就能输出全排列了   char *first = ch; char *last = ch + strlen(ch);   do {cout<< ch << endl;}while(next_permutation(first, last)); return 0;}  //这样就不必事先知道ch的大小了,是把整个ch字符串全都进行排序//若采用 while(next_permutation(ch,ch+5)); 如果只输入1562,就会产生错误,因为ch中第五个元素指向未知//若要整个字符串进行排序,参数5指的是数组的长度,不含结束符          (3) string 类型的next_permutation  int main(){ string line; while(cin>>line&&line!="#"){ if(next_permutation(line.begin(),line.end())) //从当前输入位置开始cout<<line<<endl; else cout<<"Nosuccesor\n";}}      int main(){ string line; while(cin>>line&&line!="#"){sort(line.begin(),line.end());//全排列cout<<line<<endl; while(next_permutation(line.begin(),line.end()))cout<<line<<endl;}}             next_permutation 自定义比较函数    #include<iostream> //poj 1256 Anagram#include<string>#include<algorithm>using namespace std;int cmp(char a,char b) //'A'<'a'<'B'<'b'<...<'Z'<'z'.{ if(tolower(a)!=tolower(b)) return tolower(a)<tolower(b); else return a<b;}int main(){ char ch[20]; int n;cin>>n; while(n--){scanf("%s",ch);sort(ch,ch+strlen(ch),cmp); do{printf("%s\n",ch);}while(next_permutation(ch,ch+strlen(ch),cmp));} return 0;}buaaoj230——next_permutation的应用的更多相关文章
- 关于全排列 next_permutation() 函数的用法
		
这是一个c++函数,包含在头文件<algorithm>里面,下面是基本格式. 1 int a[]; 2 do{ 3 4 }while(next_permutation(a,a+n)); 下 ...
 - About next_permutation
		
哈哈没错这个又是我们C++党的语言优势之一,用这个函数可以求当前排序的下一个排序,也就是说可以方便的求全排列,用这个函数需要用到algorithm这个头文件. 与这个函数相反的是prev_permut ...
 - STL next_permutation和prev_permutation函数
		
利用next_permutation实现全排列升序输出,从尾到头找到第一个可以交换的位置, 直接求到第一个不按升序排列的序列. #include <iostream> #include & ...
 - 【STL】next_permutation的原理和使用
		
1.碰到next_permutation(permutation:序列的意思) 今天在TC上碰到一道简单题(SRM531 - Division Two - Level One),是求给定数组不按升序排 ...
 - (转)ACM next_permutation函数
		
转自 stven_king的博客 这是一个求一个排序的下一个排列的函数,可以遍历全排列,要包含头文件<algorithm>下面是以前的笔记 (1) int 类型的next_permuta ...
 - next_permutation函数
		
这是一个求一个排序的下一个排列的函数,可以遍历全排列,要包含头文件<algorithm>下面是以前的笔记 与之完全相反的函数还有prev_permutation (1) int 类 ...
 - [算法]——全排列(Permutation)以及next_permutation
		
排列(Arrangement),简单讲是从N个不同元素中取出M个,按照一定顺序排成一列,通常用A(M,N)表示.当M=N时,称为全排列(Permutation).从数学角度讲,全排列的个数A(N,N) ...
 - [LeetCode] next_permutation
		
概念 全排列的生成算法有很多种,有递归遍例,也有循环移位法等等.C++/STL中定义的next_permutation和prev_permutation函数则是非常灵活且高效的一种方法,它被广泛的应用 ...
 - next_permutation 和 一个不成功的案例
		
一个失败的案例:(POJ 1009) 题目描述 小翔同学的宿舍WIFI添加了密码,密码每天都会变更.而小翔每天都会给蹭网的同学们提供密码提示.现在请你根据密码提示,编写程序破译密码. 已知密码提示给出 ...
 
随机推荐
- js弹出对话框的方法总结
			
九种js弹出对话框的方法总结,需要的朋友可以参考一下 [1.最基本的js弹出对话框窗口代码] 这是最基本的js弹出对话框,其实代码就几句非常简单: <script LANGUAGE=" ...
 - PHP安全性
			
一.防sql注入 用户通过输入完整的字符,来和sql语句拼接成带有破坏性的sql语句,服务器执行该语句,造成破坏. 1使用mysql_real_escape_string()过滤数据,该方法在未来版本 ...
 - [转] C语言常见笔试题大全1
			
点击阅读原文 1. 用预处理指令#define 声明一个常数,用以表明1年中有多少秒(忽略闰年问题) #define SECONDS_PER_YEAR (60 * 60 * 24 * 365UL) [ ...
 - xcode8集成百度地图(framwork包) archive是bitcode问题
			
(1)问题描述:真机和模拟器测试都能编译安装,但是需要打包archive的时候 总是编译出错,眼看就要上线了,还出现这问题,纠结啊.... 打印出来的错误: ld: bitcode bundle co ...
 - MySql_十六进制值
			
十六进制值 MySQL支持十六进制值.在数字上下文中,十六进制数如同整数(64位精度).在字符串上下文,如同二进制字符串,每对十六进制数字被转换为一个字符: mysql> SELECT x'4D ...
 - CSS 盒子模型概述
			
一.简介 CSS 盒子模型(元素框)由元素内容(content).内边距(padding).边框(border).外边距(margin)组成. 盒子模型,最里面的部分是实际内容:直接包围内 ...
 - python 学习笔记十一  SQLALchemy ORM(进阶篇)
			
SqlAlchemy ORM SQLAlchemy是Python编程语言下的一款ORM框架,该框架建立在数据库API之上,使用关系对象映射进行数据库操作,简言之便是:将对象转换成SQL,然后使用数据A ...
 - didFinishLaunchingWithOptions
			
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launc ...
 - oracle组查询
			
概念: 所谓组查询即将数据按照某列或者某些列相同的值进行分组,然后对该组的数据进行组函数运用,针对每一组返回一个结果. note: 1.组函数可以出现的位置: select子句和having 子句 2 ...
 - Struts2之Action
			
Struts2之Action MVC模式中需要有一个控制器来负责浏览器与服务器之间的通信,实现用户与服务器的交互.在Struts2框架中实现这一功能的是Action,它是整个框架最核心的部分.Acti ...