next_permutation函数和prev_permutation函数
来源:互联网 发布:网络布线施工报价 编辑:程序博客网 时间:2024/05/17 04:36
这是一个求一个排序的下一个排列的函数,可以遍历全排列,要包含头文件<algorithm>
下面是以前的笔记
(1) int 类型的next_permutation
int main()
{
a[0]=1;a[1]=2;a[2]=3;
{
cout<<a[0]<<" "<<a[1]<<" "<<a[2]<<endl;
} while (next_permutation(a,a+3)); //参数3指的是要进行排列的长度
//如果存在a之后的排列,就返回true。如果a是最后一个排列没有后继,返回false,每执行一次,a就变成它的后继
}
输出:
如果改成 while(next_permutation(a,a+2));
则输出:
只对前两个元素进行字典排序
显然,如果改成 while(next_permutation(a,a+1)); 则只输出:1 2 3
若排列本来就是最大的了没有后继,则next_permutation执行后,会对排列进行字典升序排序,相当于循环
next_permutation(list,list+3);
cout<<list[0]<<" "<<list[1]<<" "<<list[2]<<endl;
//输出: 1 2 3
(2) char 类型的next_permutation
int main()
{
cin >> ch;
sort(ch, ch + strlen(ch) );
//该语句对输入的数组进行字典升序排序。如输入9874563102 cout<<ch; 将输出0123456789,这样就能输出全排列了
cout<< ch << endl;
}while(next_permutation(first, last));
}
//这样就不必事先知道ch的大小了,是把整个ch字符串全都进行排序
//若采用 while(next_permutation(ch,ch+5)); 如果只输入1562,就会产生错误,因为ch中第五个元素指向未知
//若要整个字符串进行排序,参数5指的是数组的长度,不含结束符
(3) string 类型的next_permutation
int main()
{
{
cout<<line<<endl;
}
}
int main()
{
{
sort(line.begin(),line.end());//全排列
cout<<line<<endl;
cout<<line<<endl;
}
}
#include<iostream> //poj 1256 Anagram
#include<string>
#include<algorithm>
using namespace std;
int cmp(char a,char b) //'A'<'a'<'B'<'b'<...<'Z'<'z'.
{
}
int main()
{
cin>>n;
{
scanf("%s",ch);
sort(ch,ch+strlen(ch),cmp);
{
printf("%s\n",ch);
}while(next_permutation(ch,ch+strlen(ch),cmp));
}
}
- next_permutation函数和prev_permutation函数
- STL/next_permutation()和prev_permutation()函数
- 排列函数 prev_permutation和next_permutation的使用
- STL具体操作之next_permutation和prev_permutation函数
- STL之next_permutation和prev_permutation函数
- next_permutation和prev_permutation函数的用法
- 全排列函数next_permutation,prev_permutation
- 全排列函数 : next_permutation and prev_permutation
- C++ 求排列函数 next_permutation, prev_permutation
- 全排列函数next_permutation与prev_permutation
- 全排列函数——next_permutation() & prev_permutation()
- C++ 全排列函数 std::next_permutation与std::prev_permutation
- STL全排列函数 next_permutation prev_permutation 及拓展
- stl全排列next_permutation()与prev_permutation()函数用法
- prev_permutation 函数
- stl中的next_permutation和prev_permutation
- 黑科技(next_permutation和prev_permutation)
- next_permutation & prev_permutation
- python爬虫-网站是如何防止爬虫的
- matlab笔记2012.11.08 MATLAB打开后命令行警告
- scala代码求质数
- Redis基础、应用场景、数据结构及案例
- java解析xml文件并获取子节点
- next_permutation函数和prev_permutation函数
- Configuration--mappers--XMLMapperBuilder.parse(三-8-2)
- IT人士必备网站
- 简单视察特效的实现
- PAT basic 1050
- HDU 6165 FFF at Valentine
- canvas标签
- .NET Core 2.0 开源Office组件 NPOI
- JIRA 7.2.2破解版 服务端安装