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