Dynamic programming solves the number of combinations of all characters

Source: Internet
Author: User

One, the problem description

A number of characters are given to solve the maximum number of combinations that these characters can represent. For example {' A ', ' B ', ' C '} A total of seven combinations. (Each combination has no duplicate characters and the number of combinations is independent of the order, such as AB and BA are the same combination)

A, B, C, AB, AC, BC, ABC

In fact, the number of combinations can be solved by formula: with a given n characters, there are altogether C (n,1) +c (n,2) +...+c (n,n) different combinations. where C (n,i) means: Select the number of combinations of I from N characters, mathematically expressed as: C in.

Two, DP algorithm ideas

Now that you can use the above formula C (n,1) +c (n,2) +...+c (n,n) to calculate the number of different combinations, why use DP? Because the above formula can only be given the number of combinations, it is not possible to give specific combinations.

Assuming the input m characters (different), these characters can only form a combination of a length of,.... m, set a combination of length n. i.e.: 1 =< n <= m

Set C[m][n] Indicates the maximum number of combinations with a maximum length of n, using different characters of M.

The maximum value reflects the nature of the optimal sub-problem. The optimal sub-problem is analyzed as follows: divides the character combination into two parts, the first character and all the remaining characters.

For the M character, there are only two cases: ① is the first character of a character combination, and ② is not the first character in a character combination. Therefore, the additive principle in combinatorial mathematics can be applied. (e.g. ' abc ' is a combination of characters)

C[M][N]=C[M-1][N-1] + c[m-1][n]

C[m-1][n-1], which indicates that the first character in a character combination is the number of M characters. At this point, the problem becomes: use,... m-1 characters (m-1) to combine a character combination (' string ') of length n-1

C[m-1][n], which indicates that the first character in a character combination is not the number of M characters. At this point, the problem becomes: in the remaining m-1 characters, select n characters to combine.??? (a bit not quite right)

' m '??             .... ? The first character is a ' m '

‘*‘ ? ?             .....? The first character is not a ' m '

Note that the character combination length represented by the original problem is n

The DP solution of this problem and the dynamic programming solution have several solutions to solve the coin change problem very similar.

Third, the code implementation

 Public classAllcomposite {/**     *      * @paramStr stores the available character types *@paramm available number of characters *@parammaximum length in the N combination *@return       */     Public Static intAllcombination (Char[] str,intMintN) {                //Base Condition        if(M = = 0 && n > 0)            return1; if(n = = 0)            return0; returnAllcombination (str, m-1, n-1) + allcombination (str, m-1, N); }             Public Static intDpallcombination (Char[] str,intN) {        int[] C =New int[N+1] [N+1]; //the initial conditions can be judged according to the recursive return above         for(inti = 0; I <=n; i++) {c[0][i] = 1; c[i][0] = 0;//c[0][0]=0        }                //C[n][n]=c[n-1][m-1] + c[n-1][m]         for(inti = 1; I <=n; i++)        {             for(intj = 1; J <=n; J + +) {C[i][j]= C[i-1][j-1] + c[i-1][j]; }        }        returnC[n][n]; }         Public Static voidMain (string[] args) {Char[] str = {' A ', ' B ', ' C ', ' d '}; intm, N; M= n =str.length; intresult =allcombination (str, m, n); intRESULT2 =dpallcombination (str, n);        SYSTEM.OUT.PRINTLN (result);    System.out.println (RESULT2); }}

For the determination of initial conditions, it is possible to draw a small example diagram to determine. For example: Allcombination ({' A ', ' B ', ' C '}, 3) ...

In the code, you can print out the character combinations that are selected by the specific characters you select. In fact, I would not.

Four, references

Http://www.cnblogs.com/hapjin/p/5579737.html

Dynamic programming solves the number of combinations of all characters

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.