next_permutation函数//字典序

简介:

这是一个求一个排序的下一个排列的函数,可以遍历全排列,要包含头文件<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;
}
目录
相关文章
|
24天前
全排列----关于next_permutation()/prev_permutation() 函数的用法
全排列----关于next_permutation()/prev_permutation() 函数的用法
32 0
|
4月前
|
存储 算法 Java
实现一个函数 splice(int[] a, int b[], int n, int m) 将数组 b 插入到数组 a 的第 n 个位置上去,并将其后面的元素后移 m 个位置,同时更新数组 a 的长度
实现一个函数 splice(int[] a, int b[], int n, int m) 将数组 b 插入到数组 a 的第 n 个位置上去,并将其后面的元素后移 m 个位置,同时更新数组 a 的长度
22 0
|
12月前
|
C++
C++ 递归和非递归实现字符串反转 char *reverse(char *s)
C++ 递归和非递归实现字符串反转 char *reverse(char *s)
82 0
LeetCode 167 Two Sum II - Input array is sorted(输入已排序数组,求其中两个数的和等于给定的数)
给定一个有序数组和一个目标值 找出数组中两个成员,两者之和为目标值,并顺序输出
59 0
C#编程:用Array.Reverse反转字符串-1
C#编程:用Array.Reverse反转字符串-1
134 0
|
C语言 UED
[解题报告]【第30题】给定 n 个元素的升序整型数组,再给出一个值 target,求实现一个函数查找 nums 中 target 的下标
[解题报告]【第30题】给定 n 个元素的升序整型数组,再给出一个值 target,求实现一个函数查找 nums 中 target 的下标
HDOJ 1716 排列2 next_permutation函数
HDOJ 1716 排列2 next_permutation函数
107 0
|
Java
HDOJ 1716 排列2(next_permutation函数)
HDOJ 1716 排列2(next_permutation函数)
91 0
C#编程:用Array.Reverse反转字符串
C#编程:用Array.Reverse反转字符串
342 0
|
Java 索引 Python
LeetCode 151:给定一个字符串,逐个翻转字符串中的每个单词 Reverse Words in a String
公众号:爱写bug(ID:icodebugs) 翻转字符串里的单词 Given an input string, reverse the string word by word. 示例 1: 输入: "the sky is blue" 输出: "blue is sky the" 示例 2: 输入: " hello world! " 输出: "world! hello" 解释: 输入字符串可以在前面或者后面包含多余的空格,但是反转后的字符不能包括。
1554 0