NYOJ366(next_permutation)
来源:互联网 发布:mac ssh修改端口 编辑:程序博客网 时间:2024/05/21 10:17
这是一个求一个排序的下一个排列的函数,可以遍历全排列,要包含头文件<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就变成它的后继
next_permutation 自定义比较函数
下面是以前的笔记
(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就变成它的后继
}
(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(){ 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;}}
#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;}题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=366
#include<iostream>#include<algorithm>using namespace std;int a[]={1,2,3,4,5,6,7,8,9};int main(){int n,r;cin>>r;while(r--){ cin>>n; do { for(int i=0;i<n;i++) cout<<a[i]; cout<<endl; }while(next_permutation(a,a+n));}return 0;}
0 0
- NYOJ366(next_permutation)
- NYOJ366 D的小L next_permutation函数的用法
- next_permutation
- next_permutation
- next_permutation
- next_permutation
- next_permutation
- next_permutation
- next_permutation
- next_permutation
- next_permutation
- next_permutation
- next_permutation
- next_permutation
- next_permutation
- next_permutation
- next_permutation
- next_permutation
- 黑马程序员_设计模式SimpleFactory_11
- Imageview在百度地图中实现点击事件
- C++:STL标准入门汇总
- 好友列表右键菜单进行Ajax请求时直接跳转至登陆页面
- hadoop学习笔记3:shell下的hdfs操作
- NYOJ366(next_permutation)
- android 从activity用intent跳转到listactivity并在其中用simpleadapter在listview中显示数据列
- 选择上的一些事儿
- Linux下安装jdk-6u45-linux-x64.bin
- HDU 4282 A very hard mathematic problem(解方程 天津网络赛)
- c语言:五大基本排序算法
- 笔记本主板上的三极管与晶振的检查方法
- 整理了一些ASP初学者常用的代码
- 使用putty连接到虚拟机centos被拒绝解决方法