题目地址

简单的全排列输出,借用stl中的next_permutation就非常简单了。

关于next_permutation:(备忘,来源网络)

  1. /*这是一个求一个排序的下一个排列的函数,可以遍历全排列,要包含头文件<algorithm>
  2. 与之完全相反的函数还有prev_permutation*/
  3.  
  4. //(1) int 类型的next_permutation
  5.  
  6. int main()
  7. {
  8. int a[];
  9. a[]=;a[]=;a[]=;
  10. do
  11. {
  12. cout<<a[]<<" "<<a[]<<" "<<a[]<<endl;
  13. } while (next_permutation(a,a+)); //参数3指的是要进行排列的长度
  14.  
  15. //如果存在a之后的排列,就返回true。如果a是最后一个排列没有后继,返回false,每执行一次,a就变成它的后继
  16.  
  17. }
  18.  
  19. 输出:
  20.  
  21. 如果改成 while(next_permutation(a,a+));
  22. 则输出:
  23.  
  24. 只对前两个元素进行字典排序
  25. 显然,如果改成 while(next_permutation(a,a+)); 则只输出:
  26.  
  27. 若排列本来就是最大的了没有后继,则next_permutation执行后,会对排列进行字典升序排序,相当于循环
  28.  
  29. int list[]={,,};
  30. next_permutation(list,list+);
  31. cout<<list[]<<" "<<list[]<<" "<<list[]<<endl;
  32.  
  33. //输出: 1 2 3
  34.  
  35. () char 类型的next_permutation
  36.  
  37. int main()
  38. {
  39. char ch[];
  40. cin >> ch;
  41.  
  42. sort(ch, ch + strlen(ch) );
  43. //该语句对输入的数组进行字典升序排序。如输入9874563102 cout<<ch; 将输出0123456789,这样就能输出全排列了
  44.  
  45. char *first = ch;
  46. char *last = ch + strlen(ch);
  47.  
  48. do {
  49. cout<< ch << endl;
  50. }while(next_permutation(first, last));
  51. return ;
  52. }
  53.  
  54. //这样就不必事先知道ch的大小了,是把整个ch字符串全都进行排序
  55. //若采用 while(next_permutation(ch,ch+5)); 如果只输入1562,就会产生错误,因为ch中第五个元素指向未知
  56. //若要整个字符串进行排序,参数5指的是数组的长度,不含结束符
  57.  
  58. () string 类型的next_permutation
  59.  
  60. int main()
  61. {
  62. string line;
  63. while(cin>>line&&line!="#")
  64. {
  65. if(next_permutation(line.begin(),line.end())) //从当前输入位置开始
  66. cout<<line<<endl;
  67. else cout<<"Nosuccesor\n";
  68. }
  69. }
  70.  
  71. int main()
  72. {
  73. string line;
  74. while(cin>>line&&line!="#")
  75. {
  76. sort(line.begin(),line.end());//全排列
  77. cout<<line<<endl;
  78. while(next_permutation(line.begin(),line.end()))
  79. cout<<line<<endl;
  80. }
  81. }
  82.  
  83. next_permutation 自定义比较函数
  84.  
  85. #include<iostream> //poj 1256 Anagram
  86. #include<string>
  87. #include<algorithm>
  88. using namespace std;
  89. int cmp(char a,char b) //'A'<'a'<'B'<'b'<...<'Z'<'z'.
  90. {
  91. if(tolower(a)!=tolower(b))
  92. return tolower(a)<tolower(b);
  93. else
  94. return a<b;
  95. }
  96. int main()
  97. {
  98. char ch[];
  99. int n;
  100. cin>>n;
  101. while(n--)
  102. {
  103. scanf("%s",ch);
  104. sort(ch,ch+strlen(ch),cmp);
  105. do
  106. {
  107. printf("%s\n",ch);
  108. }while(next_permutation(ch,ch+strlen(ch),cmp));
  109. }
  110. return ;
  111. }

本题参考代码:

  1. #include <cstdio>
  2. #include <algorithm>
  3. using namespace std;
  4. int main()
  5. {
  6. int a[],l,i;
  7. while(scanf("%d",&l)&&l!=)
  8. {
  9. for(i=;i<;i++)
  10. a[i]=i+;
  11. do{
  12. for(i=;i<l;i++)
  13. printf("%d",a[i]);
  14. printf("\n");
  15. }while (next_permutation(a, a + l));
  16. printf("\n");
  17. }
  18. }
/*这是一个求一个排序的下一个排列的函数,可以遍历全排列,要包含头文件<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的应用的更多相关文章

  1. 关于全排列 next_permutation() 函数的用法

    这是一个c++函数,包含在头文件<algorithm>里面,下面是基本格式. 1 int a[]; 2 do{ 3 4 }while(next_permutation(a,a+n)); 下 ...

  2. About next_permutation

    哈哈没错这个又是我们C++党的语言优势之一,用这个函数可以求当前排序的下一个排序,也就是说可以方便的求全排列,用这个函数需要用到algorithm这个头文件. 与这个函数相反的是prev_permut ...

  3. STL next_permutation和prev_permutation函数

    利用next_permutation实现全排列升序输出,从尾到头找到第一个可以交换的位置, 直接求到第一个不按升序排列的序列. #include <iostream> #include & ...

  4. 【STL】next_permutation的原理和使用

    1.碰到next_permutation(permutation:序列的意思) 今天在TC上碰到一道简单题(SRM531 - Division Two - Level One),是求给定数组不按升序排 ...

  5. (转)ACM next_permutation函数

    转自 stven_king的博客 这是一个求一个排序的下一个排列的函数,可以遍历全排列,要包含头文件<algorithm>下面是以前的笔记  (1) int 类型的next_permuta ...

  6. next_permutation函数

    这是一个求一个排序的下一个排列的函数,可以遍历全排列,要包含头文件<algorithm>下面是以前的笔记    与之完全相反的函数还有prev_permutation  (1) int 类 ...

  7. [算法]——全排列(Permutation)以及next_permutation

    排列(Arrangement),简单讲是从N个不同元素中取出M个,按照一定顺序排成一列,通常用A(M,N)表示.当M=N时,称为全排列(Permutation).从数学角度讲,全排列的个数A(N,N) ...

  8. [LeetCode] next_permutation

    概念 全排列的生成算法有很多种,有递归遍例,也有循环移位法等等.C++/STL中定义的next_permutation和prev_permutation函数则是非常灵活且高效的一种方法,它被广泛的应用 ...

  9. next_permutation 和 一个不成功的案例

    一个失败的案例:(POJ 1009) 题目描述 小翔同学的宿舍WIFI添加了密码,密码每天都会变更.而小翔每天都会给蹭网的同学们提供密码提示.现在请你根据密码提示,编写程序破译密码. 已知密码提示给出 ...

随机推荐

  1. paper 106:图像增强方面的介绍

    图像增强是从像素到像素的操作,是以预定的方式改变图像的灰度直方图.有时又称为对比度增强,灰度变换.点运算不可能改变图像内的空间关系,输出像素的灰度值由输入像素的值决定.其作用: 对比度增强:扩展感兴趣 ...

  2. JQ 全选、全不选

    $(document).ready(function() { $("#isalldebt").click(function() { if ($(this).attr("c ...

  3. MyEclipse基础配置

    1.设置默认工作空间编码 window/preferences/general/workspace/Text file encoding 2.设置文件默认打开方式 xml建议设置 html建议设置 j ...

  4. oracle 查看某session的历史执行sql情况

    1. 查看性能最差的前100sql SELECT * FROM ( SELECT PARSING_USER_ID EXECUTIONS,SORTS,COMMAND_TYPE,DISK_READS,sq ...

  5. dr.wondr博士随笔之三星某古董智能机GTXXXX 的取证恢复一例

    大家好!欢迎来到我dr.wonde博士的微博! 这是dr.wonde的第一篇微博,不足之处,还请见谅. 今天dr.wonde给你们带来不可能的数据恢复任务之三星非智能机古董机GT-E1088C 的恢复 ...

  6. 《Vuser虚拟用户开发》读书笔记

    学会了Vuser开发只是算了性能测试入了门.要做好性能测试还需要了解系统的功能,架构和设计测试用例. 脚本选用什么协议的依据是需要模拟的客户端与服务器之间的通信采用什么协议.与具体的开发技术并无直接的 ...

  7. string类find函数返回值判定

     string类find函数返回值判定 代码示例 #include<iostream> #include<cstring> using namespace std; int m ...

  8. mac 安装phpredis扩展

    curl -O https://nodeload.github.com/nicolasff/phpredis/zip/master tar -zxf master cd phpredis-master ...

  9. Knights of the Round Table-POJ2942(双连通分量+交叉染色)

    Knights of the Round Table Description Being a knight is a very attractive career: searching for the ...

  10. 开发板A/D转换原理

    A/D转换器(Analog-to-Digital Converter)又叫模/数转换器,即使将模拟信(电压或是电流的形式)转换成数字信号.这种数字信号可让仪表,计算机外设接口或是微处理机来加以操作或是 ...