The method of using C + + to implement the whole permutation algorithm _c language

Source: Internet
Author: User
Tags int size
Copy Code code as follows:

<P> no matter which kind of total permutation generation algorithm, all follows "The original arrangement" → "The original intermediary number" → "The new intermediary number" → "The new arrangement" the process. </P><P> where the number of mediations is based on the difference between the number of increments and the number of descending digits. </P><P> proof of one by one correspondence of permutation and intermediary number we do not discuss it, here we only give a detailed mapping method of permutation and number of mediations. </P>

• Increment and decrement system numbers
The so-called increment and decrement is the number of numbers in the system with the number of different increments or decreases. Usually we see fixed numbers, such as 2Into the system,10into the system and so on.MBitNThe number of numbers that can be represented by the number of systemsM*nA. andMBit increment or decrement number can represent numbersM!A. For example, incrementing the number of systems4121, its system is from right to left, in turn,2、3、4、5。 Which is the highest bit (the number4The maximum value may be4; The third highest level may be3; The second highest may be2, the most likely to be the lowest1。 If you would4121Plus1, will make the lowest0At the same time as the second place2Add to the carry, you will also get0, at the same time as the third place.1Added to the Carry2, no more carrying. The final result is4200。 The principle of descending carry is the same, except that the system from right to left is9、8、7、6. ..., which is exactly the opposite of the increment system. Obviously, one of the greatest benefits of the descending system is that the addition is not easy to carry because it is larger in the last few (rightmost) in which the addition is most frequent.

The next thing to understand is the relationship between the increment, decrement, and its ordinal number. The increment, decrement number can be considered as an ordered set of numbers. If the ordinal number of 0 of the increment and decrement numbers is decimal 0, the 123456789 of the 987654321 and descending decimal numbers of the recursive number of digits corresponds to the decimal number 362880. (ie 9! ), you can organize a set of corresponding rules. Among them, the number of hand-enhanced Digits (A1 A2 A3 A4 A5 A6, A7 A8 A9) is:

A1*9! + a2*8! + ... + a8*2! + a9*1! = serial number

For example, the increment number of ordinal 100 is 4020, that is, the 4*4! + 0*3! + 2*2! + 0*1! = 100. Converts an ordinal to its increment number first you need to find a maximum order multiplier (i.e. 1, 2, 6, 24, 120, 720 ...) that is smaller than the ordinal number. , the integer is the first digit of the increment system, and the remainder of the division is applied repeatedly (of course, the remainder is a small order multiplier) until the remainder is 0.

The descending order number (A1 A2 A3 A4 A5 A6, A7 A8) is:

(((((a1 * 1 + a2) * 2 + a3) * 3 + ... + a7) * 8 + a8) * 9 + a9= number

For example , the descending decimal number of the ordinal number is 131 (A7 A8 A9, which is the back alignment), i.e. (1*8 + 3)*9 + 1 =. By converting an ordinal number to its descending binary number, you need to use a 9 remainder for the ordinal number to get the lowest point of the descending order (this and the increment system first calculates the highest contrast). Repeat this process with the remaining number of integers in addition to the results (of course, 8,7,6 ...). ) until the remainder is 0.

The important point to note about the increment and decrement systems is that they need to be careful when adding and subtracting, and the second is the conversion of serial numbers and numbers. In addition to the 999, the common conversions are: the increment of the number is 121211, the descending number is 1670; the increment ofthe number is 4011, and the descending number is 130. You can use this as a reference to test whether you really understand the method of calculation. The detailed calculation process for the incremental or decrement system is omitted below.

From now on, we will introduce six kinds of permutation generation algorithms in detail. Specific theoretical introductions will be ignored, and the following focus is on how to map the alignment to the number of mediations and how to restore the number of mediations to an arrangement.

I have all sought the 839647521 of the next permutation as an example.

• Incremental order generation algorithm
Mapping method:Place the original arrangement in the9To2In order to see the number of digits on the right side of it, in turn. This number is one of the number of intermediaries. For example, for the original arrangement839647521。9On the right side than9The small number has6A8On the right side than8The small number has7A7On the right side than7The small number has3A...... 2On the right side than2The small number has1A. Finally getNumber of increments in the mediation system67342221。(This number of mediations plus100The number of incremental systems4020Get a new number of mediations67351311)

Restore method: The position number of our new mediation number from left to right is 9,8,7,6,5,4,3,2. Before you restore, draw 9 spaces. For each number of mediations in position x , Count y from the right side of the space to the left without taking a space. Fill in the y+1 space with the number x. Repeat this process until all the bits in the mediation number are counted. Finally, fill in the last space of 1, complete the new arrangement of the generation. Taking the new mediation number 67351311 as an example, I give a detailed recovery step. Where the red number represents the newly filled number. Finally, we get the new arrangement 869427351.

Copy Code code as follows:

void Next_permutations_by_incredecimal (int dataarr[],int size) {
int i;
int *resultarr = new Int[size];
int index = 0;
Map<int,int>::iterator ITER;
The first step is to find the number of mediations
From large to small, gets and records the number of digits in the current arrangement that are smaller than the right side of the number I
Map<int,int> Agentmap;
For (i=0 i<size; ++i) {
Agentmap.insert (Valtype (Dataarr[i],count (dataarr,i,size,dataarr[i)));
}
Qsort (dataarr,0,size-1);
The second step is to get the new mediation number, on the basis of the old mediator number, and add 1 according to the increment method.
while (true) {
++countnum;
Next_inter_num (DATAARR,AGENTMAP);
The third step is to get a new arrangement based on the new mediation number
index = size-1;
Empties the current array of records to store the newly generated arrangement
For (i=0 i<size; ++i) {
Resultarr[i] = 0;
}
while (true) {
iter = Agentmap.find (Dataarr[index]);
Valtype value = *iter;
Resultarr[getnextposition (resultarr,size,value.second,0)] = Dataarr[index];
--index;
if (index = = 0) break;
}
The last empty position is the minimum number
i = 0;
while (true) {
if (Resultarr[i]!= 0) {
++i;
}else{
Resultarr[i] = Dataarr[index];
Break
}
}
Print (resultarr,size);
BOOL flag = TRUE;
For (I=1 i<size; ++i) {
if (Resultarr[i] > Resultarr[i-1]) {
Flag = false;
Break
}
}
if (flag) break;
}
delete [] Resultarr;
}
void Next_inter_num (int dataarr[],map<int,int>& agentmap) {
Map<int,int>::iterator ITER;
Temp current bit needs to be added, Tmpresult is the sum of the values of temp and current bit, start is the lowest starting system
int start = 2,temp=1,tmpresult;
int index = 1; The minimum number of digits in the first digit in the array
while (true) {
iter = Agentmap.find (Dataarr[index]);
Valtype value = *iter;
Tmpresult = Value.second + temp;
if (Tmpresult < start) {
No rounding has been generated
Agentmap.erase (Dataarr[index]);
Agentmap.insert (Valtype (Dataarr[index],tmpresult));
Break
}else{
Agentmap.erase (Dataarr[index]);
Agentmap.insert (Valtype (dataarr[index],tmpresult% start));
temp = Tmpresult/start;
++start;
}
++index;
}
}

• Descending order generation algorithm
Mapping method: The mapping method of the descending binary is just the opposite of the increment, that is, the9To2In order to see the number of its right side than its small number. However, the order of the number of generated mediations is no longer left-to-right, but from right to left. Generated byDecreasing number of incoming mediationsThe incremental order generation algorithm is just the mirror of the number of mediations. For example839647521The number of descending binary intermediaries is12224376。 (This number of mediations plus100Descending number of systems131The number of new mediations is obtained12224527)

Restore method: The Restore method of decreasing the number of decimal numbers is also just the opposite of the increment number of the mediation. The incremental restore method is filled in the order of the highest (left) and lowest (right) digits from the mediation number. And the descending only position reduction method counts the lowest bit of the mediation number to the highest bit to fill in the blanks in sequence. For example, for the new mediation number 12224527, I gave a detailed restore procedure. Red represents the space that is currently being filled. Finally get the new arrangement 397645821.

C + + Implementation code:

Copy Code code as follows:

void Next_permutations_by_decredecimal (int dataarr[],int size) {
Creates an array of results to record the next arrangement
int *resultarr = new Int[size];
int i;
The first step is to find the number of mediations
Map<int,int> Agentmap;
For (i=0 i<size; ++i) {
int nums = count (Dataarr,i,size,dataarr[i]);
Agentmap.insert (Valtype (dataarr[i],nums));
}
The second step of the innovation of the intermediary number here the lowest bit in the highest, it will not frequently produce carry, performance should be better than incremental carry
The lowest bit is 9, descending in turn.
int start = Size,temp = 1;
int Tmpresult;
int index = size-1;//Mediation Number The largest number in the number sequence of the lowest digits
Map<int,int>::iterator ITER;
Qsort (dataarr,0,size-1);
while (true) {
++countnum; Global variable record number of permutations
Next_inter_num (dataarr,agentmap,size);
index = size-1;
The third step is to get a new arrangement based on the number of mediations generated
For (i=0 i<size; ++i) {
Resultarr[i] = 0;
}
while (true) {
iter = Agentmap.find (Dataarr[index]);
Valtype value = *iter;
Find Next fill vacancy
Resultarr[getnextposition (resultarr,size,value.second,0)] = Dataarr[index];
--index;
if (index = = 0) break;
}
i = 0;
while (true) {
if (Resultarr[i]!= 0) {
++i;
}else{
Resultarr[i] = Dataarr[index];
Break
}
}
Print (resultarr,size);
BOOL flag = TRUE;
For (I=1 i<size; ++i) {
if (Resultarr[i] > Resultarr[i-1]) {
Flag = false;
Break
}
}
if (flag) break;
}
delete [] Resultarr;
}
void Next_inter_num (int dataarr[],map<int,int> &agentmap,int size) {
int start = Size,temp = 1;
int Tmpresult;
int index = size-1;//Mediation Number The largest number in the number sequence of the lowest digits
Map<int,int>::iterator ITER;
while (true) {
iter = Agentmap.find (Dataarr[index]);
Valtype value = *iter;
Tmpresult = Value.second + temp;
if (Tmpresult < start) {
Do not produce carry, directly rewrite the bottom value
Agentmap.erase (Dataarr[index]);
Agentmap.insert (Valtype (Dataarr[index],tmpresult));
Break
}else{
Produce a rounding
Agentmap.erase (Dataarr[index]);
Agentmap.insert (Valtype (dataarr[index],tmpresult% start));
Tmpresult = Tmpresult/start;
--start;
}
--index;
}
}

• Dictionary full Array generation method
Mapping method:The original arrangement of numbers from left to right (the end is ignored), in turn to see the number on the right side than its small number of several, the number is the number of intermediaries. For example, for an arrangement839647521。 Highest level8Right ratio8The small one has7A number, a secondary high3Right ratio3The small number has2One, again bit of9On the right side than9The small one has6A number,......,2On the right side than2The small number has1A. GetNumber of increments in the mediation system72642321。 (This number of mediations plus100Number of incremental Feed4020The number of new mediations is obtained72652011)

Restore method: The Restore method is a reverse procedure for the mapping method. You can write the secondary number 1 2 3 4 5 6 7 8 9 First, and the 9 empty space to populate the new arrangement. Then the highest digit of the new mediation number. For example, the highest bit of the new mediation is x , and you can count to x from the first number in the secondary number that is not being used. The x+1 number should be the first number of vacancies. We marked this number as already used and then filled it with the first empty space on the left. Then, look at the secondary high y of the new mediation number, counting from the first unused number of the secondary number to one. The first y+1 number is the number of the next vacancy. We marked this number as already used , and then filled it with the second empty space on the left. And so on, until the number in the last mediation number is counted. For example, for the new mediation number 72652011 , we give the secondary number and each step of the vacancy. Where the red numbers represent being marked as used , the number of with is no longer counted in the subsequent count. When all the numbers in the new mediation number are counted, the only remaining digits in the secondary number will be filled in the final vacancy. Finally get the new arrangement 839741562 . /span>


C + + implementation:
Copy Code code as follows:

void Next_permutations_by_dicorder (int dataarr[],int size) {
int key = 0;
int index,temp,end,left,right;
int i;
BOOL Flag;
while (true) {
++countnum;
Print (dataarr,size);
Flag = true;
index = 0,temp = 0,end=8,left = right = 0;
Find the first drop point from the end of the current arrangement
for (i = size-1 i > 0; i--) {
if (Dataarr[i] > Dataarr[i-1]) {
key = I-1; K Record Drop Point
Flag = false;
Break
}
}
If it is already from high to low order, the operation completes
if (flag)
Break
index = key + 1;
Find the smallest number in the suffix that is larger than the first drop point
while (Dataarr[key] < Dataarr[index] && Index < size) {
++index;
}
Index--;
The number to be found and the first occurrence of a drop in the exchange of points
temp = Dataarr[key];
Dataarr[key] = Dataarr[index];
Dataarr[index] = temp;
left = key+1;
right = size-1;
To reverse the number behind the drop point.
while (left < right) {
temp = Dataarr[left];
Dataarr[left] = Dataarr[right];
Dataarr[right] = temp;
++left;
--right;
}
}
}

Backtracking method:
Copy Code code as follows:

void Next_permutations_by_backtrack (int dataarr[],int size) {
Create an array of results
int *resultarr = new Int[size+1];
BackTrack (1,size+1,resultarr,dataarr);
delete [] Resultarr;
}
Pruning function
BOOL Place (int k,int resultarr[])
{
for (int j = 1; j < K; J + +) {
if (resultarr[j] = = Resultarr[k]) {
return false;
}
}
return true;
}
void BackTrack (int t,int size,int resultarr[],int dataarr[])
{
if (T > size-1) {
+ + Countnum;
for (int i = 1; i < size; i++) {
cout << Resultarr[i] << "";
}
cout << Endl;
} else {
for (int i = 1; i < size; i++) {
RESULTARR[T] = dataarr[i-1];
if (place (T,resultarr)) {
BackTrack (T+1,size,resultarr,dataarr);
}
}
}
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.