This is a function that asks for a sort of next permutation, which can traverse the entire arrangement, to include the header file <algorithm>
The following is the exact inverse of the previous notes and the Prev_permutation, which is the dictionary that the query is currently sorted on.
Returns to bool type, returns TRUE if successful, returns false fails, restores to ascending or descending order, and is better with sort
(1) Next_permutation of type int
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)); Parameter 3 refers to the length to be arranged
Returns true if there is an array after a. If A is the last permutation without a successor, returns false, each time it executes, a becomes its successor
}
Output:
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
If changed to while (Next_permutation (a,a+2));
The output:
1 2 3
2 1 3
Sort only the first two elements in a dictionary
Obviously, if you change to while (Next_permutation (a,a+1)); Then output only: 1 2 3
If the arrangement is the largest, there is no successor, then after the execution of Next_permutation, the arrangement will be sorted in ascending order of the dictionary, equivalent to the loop
int list[3]={3,2,1};
Next_permutation (list,list+3);
cout<<list[0]<< "" <<list[1]<< "" <<list[2]<<endl;
Output: 1 2 3
(2) Char type next_permutation
int main ()
{
Char ch[205];
CIN >> ch;
Sort (CH, ch + strlen (ch));
The statement sorts the input array in ascending order. If the input 9874563102cout<<ch; will output 0123456789, so that the output can be fully arranged
char *first = ch;
Char *last = ch + strlen (CH);
do {
cout<< ch<< Endl;
}while (Next_permutation (First, last));
return 0;
}
So that you do not have to know the size of CH, is the entire CH string all sorted
If you use while (Next_permutation (ch,ch+5)), if you enter only 1562, an error occurs because the Fifth element in Ch points to an unknown
To sort the entire string, parameter 5 refers to the length of the array, without the Terminator
(3) Next_permutation of type string
int main ()
{
String line;
while (cin>>line&&line!= "#")
{
if (Next_permutation (Line.begin (), Line.end ()))//start at the current input position
cout<<line<<endl;
elsecout<< "nosuccesor\n";
}
}
int main ()
{
String line;
while (cin>>line&&line!= "#")
{
Sort (Line.begin (), Line.end ());//full arrangement
cout<<line<<endl;
while (Next_permutation (Line.begin (), Line.end ()))
cout<<line<<endl;
}
}
Next_permutation a custom comparison function
#include <iostream>//poj 1256Anagram
#include <string>
#include <algorithm>
using namespace Std;
int cmp (char A,char B)//' A ' < ' a ' < ' B ' < ' B ' <...< ' z ' < ' z '.
{
if (ToLower (a)!=tolower (b))
Returntolower (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;
}
Transfer from http://blog.sina.com.cn/s/blog_9f7ea4390101101u.html
"Go" next_permutation and prev_permutation (full permutation function in STL Library) usage