Select m from n elements and arrange m <n (not all !!!) Recursive algorithm code

Source: Internet
Author: User

M (m ≤ n) elements from n different elements are arranged in a certain order, which is called an arrangement of M elements from n different elements. When M = N, all data is arranged in full order. Recursive algorithm code in full order is everywhere on the Internet, but when m
<N, the Code cannot work. I add the const int selected_count parameter to the recursive algorithm to indicate the number of elements involved in the arrangement. (If you have any questions, in this Code, when only one element is selected, that is, selected_count = 1. If the final arrangement result set does not use the set data structure or other methods to remove duplicate elements, there will be duplicates, but when the number of elements involved in the arrangement is selected_count
> 1. There will be no repeated elements. If someone else does not give me some advice, I am very grateful !)

PS: I want to know the mathematical derivation of the fully arranged recursive solution, and use recursion to get the full arrangement of a set. This online code is everywhere. However, it seems that few people have explained the mathematical principle, we found a seemingly less rigorous derivation:

Set a group of numbers P = {R1, R2, R3,..., Rn}. The full row column is perm (P), Pn = p-{Rn }.
Therefore, Perm (p) = r1perm (P1), r2perm (P2), r3perm (P3),..., rnperm (PN ). When n = 1, Perm (p} = R1. "(From: http://yjglg.blog.163.com/blog/static/72321012201211471649830/), I do not know which one has a more rigorous mathematical derivation process ???

// Some code reference: http://blog.csdn.net/yxjiang/article/details/3013574
// Recursive version: This is the most easy-to-understand version. However, the 26th lines of code circulating on the Internet are not commented out. I have read that the running results are the same,
// The Order is different. Versions circulating on the Internet do not appear in an ascending order of output because they are exchanged after each exchange.
// The output order of commented versions is incremental.
// The following code is used:

/*** Permutation helper. * @ Param STR original stringsselected_count the count of the elements for permutation, shocould <n * @ return all the permutations */void permutation_generator (string STR, int M, int N, set <string> & result, const int selected_count) {If (selected_count> N + 1 | selected_count <= 0) {cout <"error! Selected_count, the count of the elements for permutation, shocould <"<n + 1 <" &> 0 "<Endl; return;} If (M> N) {string res = Str. substr (0, selected_count); result. insert (RES); // cout <res <Endl; // when selected_count = 1, print out repeated elements} else {for (INT I = m; I <= N; ++ I) {swap <char> (STR [m], STR [I]); permutation_generator (STR, m + 1, n, result, selected_count); // swap <char> (STR [m], STR [I]); }}}/* Print the permutation result set */void printpermutresltset (const set <string> & res) {set <string >:: const_iterator ITER; For (iter = res. begin (); iter! = Res. end (); ITER ++) cout <* ITER <Endl;} int _ tmain (INT argc, _ tchar * argv []) {set <string> res; string STR = "ABC"; permutation_generator (STR, 0, str. size ()-1, res, 1); printpermutresltset (RES); System ("pause"); Return 0 ;}

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.