Summary of permutation and combination recursive and non-Recursive Algorithms

Source: Internet
Author: User

Summary of permutation and combination recursive and non-Recursive Algorithms


# Include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
# Include 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
     
      
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
      
        Result; for (int I = 0; I <5; I ++) flag [I] = false; cout <"--- recursive combination 1 (print each Combination Based on the flag bit) ---- "<endl; Comb (4, 3); cout <" --- recursive combination 2 (save each combination to the container) --- "<endl; Comb2 (4, 1 ); cout <"--- non-recursive combination ---" <endl; 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 (a)/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 with epoll for (int I = 0; I
       
         : Iterator it; for (it = result. begin (); it <result. end (); it ++) // cout <* it <"; cout <endl; 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
        
          > J) & 1) // tips ----: bit cout with a value of 1 <
         
           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
          
            (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
           
             (Cout, ""); cout <endl ;}} void stl_permutaton (int * a, int len) {// the first sequence of the Lexicographic Order must be incremented by sort (, a + len); do {copy (a, a + len, ostream_iterator
            
              (Cout, ""); cout <endl ;}while (next_permutation (a, a + len);}/* Train of Thought: (A, B, C, D) column 1, column A followed by (B, C, D) in full arrangement 2, column B followed by (A, C, D) (A, B, and D). (4). (A, B, and C) and the (B, C, D) in 1 can still be decomposed 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 <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
             
               (Cout, ""); // cout <
              
               





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.