Introduction to the algorithm Classic-seventh chapter example 7-2-2 the permutation of the re-set

Source: Internet
Author: User

Heavy: If the problem turns into an input array p and outputs all the full permutations of the array a elements in the dictionary order, you need to modify the full arrangement of the code set
//Rujia Liu#include <cstdio>#include<algorithm>using namespacestd;intp[ -], a[ -];//the full arrangement of elements in the output array p. There may be duplicate elements in the array PvoidPrint_permutation (intNintPintAintcur) {  if(cur = =N) { for(inti =0; I < n; i++) printf ("%d", A[i]); printf ("\ n");
printf ("cur=%d\n", cur); } Else for(inti =0; I < n; i++) //P[i] = = P[i-1], then p[i] This number only consider p[i-1] on the line//i = = 0 o'clock so be sure to take it because there is no more before it can be considered if(!i | | P[i]! = p[i-1]) { intC1 =0, C2 =0; //A is already filled in, p is not filled in//use C1 c2 statistics have been filled in the number and all, if C1 < C2 description has not filled in for(intj =0; J < cur; J + +) if(A[j] = = P[i]) c1++; for(intj =0; J < N; J + +) if(P[i] = = P[j]) c2++;//printf ("i=%d cur=%d c1=%d c2=%d\n", I, CUR,C1,C2); if(C1 <C2) {A[cur] =P[i]; Print_permutation (n, P, A, cur+1); } }}intMain () {intI, N; scanf ("%d", &N); for(i =0; I < n; i++) scanf ("%d", &P[i]); Sort (p, p+N); Print_permutation (n, P, A,0); return 0;}

Test

Introduction to the algorithm Classic-seventh chapter example 7-2-2 the permutation of the re-set

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.