Permutation and combination recursive and non-recursive algorithms Summary
# Include <iostream> # include <string> # include <math. h ># include <vector >#include <algorithm> using namespace std; // method1bool flag [5]; int arr [5] = {1, 2, 3, 4, 5 }; int len = sizeof (arr)/sizeof (int); void Comb (int n, int count); // method2void Comb2 (int n, int count ); vector <int> result; // method3: void Comb3 (int n, int count); int GetCombCount (int n, int m); // 3 void GetCharComb (); // 4int data [3] = {1, 2, 3}; int data1 [3] = {4, 2, 3}; void permutation (int * a, int len); void stl_permutaton (int * a, int len ); // you can obtain Comb () + empty set // 5int num = 0; void permutation (int array [], int begin, int end); int main () {vector <char> result; for (int I = 0; I <5; I ++) flag [I] = false; cout <"--- recursive combination 1 (print each Combination Based on the flag) ----" <endl; Comb ); cout <"--- recursive combination 2 (save each combination to the container) ---" <endl; Comb2 (); cout <"--- non-recursive combination ---" <end L; Comb3 (5, 3); cout <"--- all combinations of strings (idempotent except empty strings) ----" <endl; GetCharComb (); cout <"--- non-recursive arrangement ---" <endl; permutation (data, 3); cout <"---- STL full arrangement --" <endl; stl_permutaton (data1, 3); cout <"-- Recursive arrangement ----" <endl; int a [3] = {2, 3, 4}; permutation (a, 0, sizeof () /sizeof (int)-1); return 0;} void Comb (int n, int count) {if (count = 0) {// simiar with select using vector storage selected data will be similar Epoll for (int I = 0; I <len; I ++) if (flag [I] = true) cout <arr [I] <""; cout <endl; return; //-exit condition 1 -- (recursive end condition 1)} if (n <0) // exit condition 2 -- (recursive end condition 2) return; flag [n] = true; Comb (n-1, count-1); flag [n] = false; Comb (n-1, count);} void Comb2 (int n, int count) {if (count = 0) {// a combination obtained after each recursive end, vector <int>: iterator it; for (it = result. begin (); it <result. end (); it ++) // cout <* it <""; cout <end L; return;} if (n <0) return; result. push_back (arr [n]); Comb2 (n-1, count-1); result. pop_back (); Comb2 (n-1, count);}/* use an array to simulate a binary calculator. If one is 1, the corresponding character is obtained. If the value is 0, no value is obtained, character combinations can be implemented. You can also remove arrays. It can contain n characters. Int num is increased from 1 to 2 ^ n-1, and num is shifted to I-bit right, followed by 1 to perform the bitwise & operation to determine whether to take the I character or not. 001 010 011 100 101 110 111c B bc a ac AB abc // the problem still exists is the length of the character array <32, this method is still very good, if it is greater than 32, the original method is required. */Void GetCharComb () {// evaluate the idempotent set // string str = "aabc"; string str = "abc"; int N = str. size (); int num = pow (2.0, N); // (2.0 N) for (int I = 1; I <num; I ++) // num = [1-7] {for (int j = 0; j <N; j ++) {if (I> j) & 1) // tips ----: bit cout detected as 1 <str [j]; // ---- str [j] is first a} cout <endl ;}} /* lexicographic changes (1234 ----> 4321) 1. compare the adjacent elements from the rightmost side until find the two adjacent elements on the right that are larger than the left. is to be replaced, and then find the first one greater than this element from the rightmost side, and exchange the two three. after switching, flip all elements after the switching element */void permutation (int * a, int len) {Int I, j; int tmp; int num = 6; // 1*2*3 copy (arr, arr + len, ostream_iterator <int, char> (cout, ""); cout <endl; for (I = len-1; I> 0; I --) {if (a [I]> a [I-1]) // I-1 [1] {for (j = len-1; j> = 0; j --) if (a [j]> a [I-1]) // [2] {tmp = a [I-1]; a [I-1] = a [j]; a [j] = tmp; // [3] int m = I, n = len-1; while (m <= n) {tmp = a [m]; a [m] = a [n]; a [n] = tmp; m ++; n --;} break;} I = len; // begin againcopy (a, a + len, ostream_iterator <int, char> (cout, ""); cou T <endl ;}}void stl_permutaton (int * a, int len) {// the first sequence of the Lexicographic Order must be incremented by sort (a, a + len ); do {copy (a, a + len, ostream_iterator <int, char> (cout, ""); cout <endl ;}while (next_permutation (, a + len);}/* Train of Thought: (A, B, C, D) The full row is column 1, followed by A (B, C, D) B and C (A, C, D). 3. C and C (A, B, and D) the full arrangement of 4, D followed by (A, B, C) and the (B, C, D) of 1 can still be divided according to the above form. */Void permutation (int array [], int begin, int end) {int I; if (begin = end) {// recursive exit condition processing the last element has num ++; // cout <end <endl; for (I = 0; I <= end; ++ I) {cout <array [I] <";}cout <endl; return ;} else {// for Loop traverses all possible conditions at the first position in the arrangement for (I = begin; I <= end; ++ I) {swap (array [I], array [begin]); permutation (array, begin + 1, end); swap (array [I], array [begin]); // restore the array to continue the next traversal }}// Non-Recursive method to get combinationvoid Comb3 (int n, int count) {/* Minimum variation of decreasing: find the first 10-to-10 switch from the right to the left, indicating that I want to reduce the number but I want to reduce the number at least, then the number behind the switch point should be as large as possible, so move all 1 to the high position after the switching point 11100110101100110110101011001101110011010101100111 * // int bit [5] = {, 0 }; // initial bit arrayint * bit = new int [n]; for (int k = 0; k <n; k ++) {if (k <count) bit [k] = 1; elsebit [k] = 0;} int I, j, beg, end; int len = sizeof (arr)/sizeof (int ); int N = GetCombCount (n, count); // C (n, count) C (5, 3) for (I = 0; I <len; I ++) if (bit [I] = 1) cout <arr [I]; cout <endl; for (j = 1; j <= N-1; j ++) {for (I = len-1; I> 0; I --) {if (bit [I] = 0 & bit [I-1] = 1) {swap (bit [I], bit [I-1]); // from index: [I to len-1], make all bit 1 in the rightbeg = I; end = len-1; while (1) {while (bit [beg] = 1) {beg ++; if (beg> = len) break ;} while (bit [end] = 0) {end --; if (end <I) break;} if (beg <end) swap (bit [beg], bit [end]); elsebreak;} // end of "while" break;} // end of "if" // copy (bit, bit + 5, ostream_iterator <int, char> (cout, ""); // cout <endl ;}for (I = 0; I <len; I ++) if (bit [I] = 1) cout <arr [I]; cout <endl ;}} int GetCombCount (int n, int m) {int I; int a, B, c, s; // s = a/(B * c) a = B = c = 1; for (I = 1; I <= n; I ++) a * = I; for (I = 1; I <= m; I ++) B * = I; for (I = 1; I <= n-m; I ++) c * = I; s = a/(B * c); return s ;}
Recursive arrangement and combination
The same is true for C of.
Like a class,
Int f (int n)
Static int s;
If (n> 1)
{S * = n;
Return f (n-1 );}
Else
Return s;
If not An, n
You define the condition
How to implement this vb algorithm? (Permutation and combination and Recursion can be used)
This is actually a step-by-step method in an arrangement and combination. The following code uses recursive methods for your reference:
Sub main () Dim srcTxt $, myAry (), tmp, UB % '------ raw data is input to the array ----------- srcTxt = "cap, tat, nbc-abc, mnt, xxs-oop "tmp = Split (srcTxt,"-") UB = UBound (tmp) ReDim myAry (UB) For I = 0 To UBmyAry (I) = Split (tmp (I), ",") Next I '---- call Recursion TO FIND THE COMBINATION ------ Dim x %, p (), PM (), pNo & ReDim p (UB) getPmt x, p (), pNo, PM (), myAry () '-------- output --------- MsgBox "possible combinations include:" & vbCrLf & Join (PM, vbCrLf ), vbInformationEnd Sub ''recursively calculates the combination and puts the result into the PM () array Sub GetPmt (ByVal x %, p (), pNo &, PM (), myAry ()) for I = 0 To UBound (myAry (x) p (x) = myAry (x) (I) If x = UBound (myAry) ThenReDim Preserve PM (pNo) PM (pNo) = Join (p, "") pNo = pNo + 1 ElseGetPmt x + 1, p (), pNo, PM (), myAry () End IfNext iEnd Sub