Produces an array of fully arranged, non-redundant C + + implementations

Source: Internet
Author: User

Finds all the permutaitons of n elements, repeated elements is allowed and do not create redundant permutations.

Proteomics Ms-topdown Source code auxfun.cpp a function, looked for a long time without comments, a bit dizzy, but can be used directly in the future.

The original array elements are all arranged, but the requirements are non-redundant, that is, the original array can have duplicate elements, such as {2,2,2} This array, there is only one arrangement, 2 this element must be treated the same, but the number is 3

1 voidGenerate_all_permutations (Constvector<int>&Org_vector,2vector< vector<int> >&permutations)3 {4Unsignedinti;5vector<int>counts, symbols;6 permutations.clear ();7 8     if(org_vector.size () = =0)9         return;Ten  One counts.clear (); A symbols.clear (); -  -     //create vector with symbols and their counts theSymbols.push_back (org_vector[0]); -Counts.push_back (1); -  -      for(i=1; I<org_vector.size (); i++) +     { -UnsignedintJ; +          for(j=0; J<counts.size (); J + +) A         { at             if(Org_vector[i] = =Symbols[j]) -             { -counts[j]++; -                  Break; -             } -         } in  -         if(J = =counts.size ()) to         { + Symbols.push_back (Org_vector[i]); -Counts.push_back (1); the         } *     } $ Panax Notoginsengvector<int>next_sym_idx,perm; -     intn = org_vector.size ();//Total number of elements the     intK = Counts.size ();//Total number of element types +Next_sym_idx.resize (N,0); APerm.resize (n,-1); the     intD=0; +  -      while(1) $     { $          while(Next_sym_idx[d]<k && counts[next_sym_idx[d]] = =0) -next_sym_idx[d]++; -  the         if(next_sym_idx[0]==k) -              Break;Wuyi  the         if(Next_sym_idx[d] >=k) -         { Wunext_sym_idx[d]=0; -d--; Aboutcounts[next_sym_idx[d]]++; $next_sym_idx[d]++; -             Continue; -         } -  A         //Add Symbol +perm[d]=Symbols[next_sym_idx[d]]; thecounts[next_sym_idx[d]]--; -d++; $  the         if(d = =N) the         { the Permutations.push_back (perm); the     //int k; -     //For (k=0; k<perm.size (); k++) in     //cout << perm[k] << ""; the     //cout << Endl; the  Aboutd--; thecounts[next_sym_idx[d]]++; thenext_sym_idx[d]++; the         } +     } -}

Produces an array of fully arranged, non-redundant C + + implementations

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.