The whole arrangement of repeating elements for reducing and treating

Source: Internet
Author: User
Tags repetition

All solutions of n elements can be solved by the method of reduction: each time a number is prefixed, the remaining elements are perfection, until only one element is left. code from algorithmic analysis and design (Wang Xiaodong), Complexity O (n2)

1 //all permutations of the output k~m2 voidPermintKintm)3 {4     if(k==m)5     {6          for(intI=0; i<=m;i++)7printf"%d", List[i]);8printf"\ n");9}ElseTen     { One          for(inti=k;i<=m;i++) A         { - swap (list[k],list[i]); -Perm (k +1, m); the swap (list[k],list[i]); -         } -     } -}

The above does not consider the case of duplicate elements. Simply think, if there is a repetition of the element, then this element can only be carried out to do a prefix, then just before carrying the prefix to determine whether the number has been lifted out.

So how do you judge it effectively? It is possible to sort all the elements first, so that the repeating elements are distributed in adjacent locations and scanned, only the elements that are different from the precursor are processed.

The code simply adds an if (i>k&&list[i]==list[i-1]) continue within the k~m loop;

Let's look at a similar question.

UVA11076 http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=33478

An array of n digits (repeatable) in 0~9, each of which is considered to be an n-digit number, and all permutations are ordered.

According to the whole arrangement of the character, all the rows are written out by the row, found that each column "All numbers and S" are equal, so we can only ask for the sum of any column and then n times the *10 accumulation.

For each number k, we know how many times it appears in the array Cnt[k] (that is, K has cnt[k]-1 copies), but to sum a column (or 1th column), we need to be aware of the number of times each number appears in this column Cnt_2[k]. Since the whole arrangement is not the same, then the number of "1th is K" is equivalent to "the number of the remainder of the remaining elements after the K is removed", at this point, the problem is converted to the above perfection arrangement of the reduction method.

The subject simply perfection the number of permutations (values) without having to output a specific arrangement (solution), so you can use the classic method of arranging the combination in high school: first as a non-repeating full row, divided by the number of permutations of all repeating elements.

After the discovery of the problem because the number is 0~9, so naturally the elements are ordered and recorded a repetition of the number of times, so that each number k is calculated once, the number of K as the calculation of cnt[k]-1 can be.

The code is as follows:

1#include <cstdio>2#include <cstring>3 using namespacestd;4 5typedef unsignedLong LongULL;6 //only 0~97ULL fac[]={1,1,2,6, -, -,720,5040,40320,362880,3628800,39916800,479001600};8 inta[ -];9 intcnt[Ten];//Number of occurrencesTen intcnt_2[Ten];//The number of occurrences in any column that is fully arranged One ULL sum,ans,s; A intN; -  - intMain () the { -      while(SCANF ("%d", &n) &&N) -     { -memset (CNT,0,sizeof(CNT)); +sum=0; -          for(intI=0; i<n;i++) +         { Ascanf"%d",&a[i]); atsum+=A[i]; -cnt[a[i]]++; -         } -s=0; -          for(intI=0; i<=9; i++) -{//Cnt_2[i] equals the number of all permutations without repeating one I in             if(cnt[i]==0)Continue; -cnt_2[i]=fac[n-1]; to              for(intj=0; j<=9; j + +) +             { -                 if(cnt[i]==0)Continue; the                 if(I==J) cnt_2[i]/=fac[cnt[i]-1]; *                 ElseCnt_2[i]/=Fac[cnt[j]]; $             }Panax Notoginsengs+=i*Cnt_2[i]; -         } theans=0; +          for(intI=0; i<n;i++) A         { theans+=s; +s*=Ten; -         } $printf"%llu\n", ans); $     } -     return 0; -}

Note: Before I have been dizzy, to re-realize not as a swap problem, that the simple exchange I and K will destroy the "repeated element concentration distribution" or "ordered sequence" two conditions, but the analysis found that there is no relationship ... To reduce the rule, the prefix is carried away on the suffix of the full alignment has not affected, as long as the repetition of each cycle to ensure that the elements are not carried to the same position.

From the problem itself and the idea of the algorithm to analyze it is very interesting ~ algorithmic knowledge is broad and profound, I hope to practice more accumulation, as soon as not so water ~ ~ ~

The whole arrangement of repeating elements for reducing and treating

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.