The method of division and treatment of classical algorithm learning (taking permutation, combination program as an example)

Source: Internet
Author: User

Divide and conquer the idea of the law: The original problem is divided into several smaller but similar to the original problem of sub-problem, the recursive solution of these sub-problems, and then merge the solutions of these sub-problems to establish the solution of the original problem.

The divide-and-conquer approach is followed in three steps for each level of recursion:

(1) decomposition of the original problem is a number of sub-problems, these sub-problems are small instances of the original problem.

(2) To solve these sub-problems, the team rules to solve each sub-problem, when the sub-problem size is small enough, the direct solution.

(3) The solution of merging These sub-problems constitutes the solution of the original problem.

Obviously the merge sort is an example of a very classical rule of division, given that a post on merge sort has been written before, and this is not the case with merge ordering.

Note that the first step in each layer of the division of the decomposition of the recursive, may produce two sub-problems (such as merge sort, binary search, etc.), may also produce many sub-problems (such as permutations, combinations, etc.), the generation of two sub-problems of course is relatively easy to understand, and produce multiple sub-problems need to use the ring Follow List these sub-problems.

The following is an example of permutation and combination algorithm, which introduces the algorithm of divide and conquer to produce many sub-problems.

First, arrange

Problem: Enter a string to print out all the permutations of the characters in the string.

Analysis: Using the idea of divide-and-conquer law,

(1) First the original problem decomposition, if the input string length is N, then the first choice may be the first character, may be the second 、。。。 may also be nth, but whichever one, just select the first character, you can continue to choose one of the remaining n-1 characters, so the original problem needs to be decomposed into N sub-problem (each sub-problem is the first choice of the I, and then the other than I in the full array), It is now possible to find that if the characters except I are fully arranged after the sequence decomposition, it is not so easy to implement recursion, so I think that each element (including the first element) is exchanged with the first element, then the sub-problem is to first exchange and select each element with the first element, Then all the elements of the second to the last are arranged in a full order. Note that the swapped elements need to be swapped back after each sub-problem is considered.

(2) Solving each sub-problem with recursion

(3) When all the problems are solved, the solution of the sub-problem is the solution of the original problem.

For example: The input string is ABC, the permutation function is permutation () then the sub-problem is a+permutation (BC), B+permutation (AC), c+permutation (AB)

1#include"stdafx.h"2#include <iostream>3 using namespacestd;4 voidPrintChar*str)5 {6     Char*p=str;7      while(*p)8     {9cout<<*p<<' ';Tenp++; One     } A } - voidBianli (Char*STR,intBeginintlength) - { the     Chartemp; -     inti; -     if(begin==length-1) -     { + print (str); -cout<<Endl; +         return ; A     } at     //You can select a value (including begin yourself) to exchange the position with begin and then embox the rest of the characters -     //so for each position you choose to Exchange First, then recursion, or choose not to swap (that is, swap two times) -      for(i=begin;i<length;i++) -     { -temp=Str[begin]; -str[begin]=Str[i]; instr[i]=temp; -  toBianli (str,begin+1, length); +  -temp=Str[begin]; thestr[begin]=Str[i]; *str[i]=temp; $     }Panax Notoginseng } -  the int_tmain (intARGC, _tchar*argv[]) + { A     Charstr[4]="123"; theBianli (str,0,3); +     return 0; -}

Two, the combination

Problem: Find out from the natural number 1, 2, 3 ... Any combination of r elements in n

Analysis:

1, decomposition: different from the arrangement, the combination of each element in a single occurrence, so do not need to exchange elements, but each time from the n number in a certain order to take an element, and then consider a comprehensive, such as a maximum value each time, so long as the number of elements >k is a sub-problem, The rest of the thoughts and permutations are similar.

1#include <iostream>2 using namespacestd;3 inta[ -];//results for storing the combination4 voidZuhe (intNintk)5 {6      for(inti=n;i>=k;i--)//the largest number in a sequential selection combination7     {8a[k]=i;9         if(k>1)Ten         { OneZuhe (I-1, K-1); A         } -         Else -         { the              for(intI=1; i<=a[0];i++) -             { -cout<<a[i]<<" "; -             } +cout<<Endl; -         } +     } A } at intMain () - { -     intn,k; -Cin>>n>>K; -a[0]=K; - Zuhe (n,k); in     return 0; -}

The method of division and treatment of classical algorithm learning (taking permutation, combination program as an example)

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.