The next_permutation and prev_permutation functions defined in C++/stl are a very flexible and efficient method that is widely used to generate different permutations for the specified sequence.
The Next_permutation function generates the next larger arrangement of the given sequence in alphabetical order until the entire sequence is descending.
The Prev_permutation function, in contrast, is the generation of a smaller arrangement for a given sequence.
The so-called "next" and "previous", give a simple example:
For the sequence {A, B, c}, each element is smaller than the back, following the dictionary sequence, A is smaller than BC after fixing a, c is larger than B, and its next sequence is {A, C, b}, whereas the previous sequence of {A, C, b} is {A, B, C}, and the same can be introduced for all six sequences: {A, B, c }, {A, C, b}, {B, A, c}, {B, C, a}, {C, a, b}, {C, B, a}, where {A, B, C} have no previous element, {C, B, and a} have no next element.
The two principles are the same, only in the case of the reverse order, here only next_permutation as an example to introduce the algorithm.
(1) Next_permutation of type int
intMain () {inta[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 you change into
1 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
1 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
1 int list[3]={3,2,1}; 2 next_permutation (list,list+3); 3 cout<<list[0]<<""<<list[1]<< " "<<list[2]<<endl;
Output: 1 2 3
(2) Char type next_permutation
intMain () {Charch[205]; CIN>>ch; Sort (CH, ch+strlen (CH)); //the statement sorts the input array in ascending order. such as input 9874563102cout<<ch;//the output will be 0123456789 so that the output is fully aligned. 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// start cout<<line<< Endl from the current input position ; Else cout<<"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 1256 Anagram#include <cstring>#include<algorithm>using namespacestd;intcmpCharACharb//' A ' < ' a ' < ' B ' < ' B ' <...< ' z ' < ' z '.{ if(ToLower (a)! =ToLower (b))returnToLower (a) <ToLower (b); Else returna<b;}intMain () {Charch[ -]; intN; 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;}
It is convenient to use next_permutation and prev_permutation to arrange the combination, but remember to include the header file # include <algorithm>.
Although the last permutation does not have the next arrangement, Next_permutation returns false, but with this method, the sequence becomes the first of the dictionary sequence, such as the CBA becomes ABC. Prev_permutation.
"STL" full array generation algorithm: Next_permutation