Recurrence page 20
[Example 1] Division
Set S is a collection of n elements
Sub set SL, S Z, and. , S K:
1. Si 56 call
(Al, A z, ·., a.), now S collection into K satisfaction
2. S; door SJ = 69 '
3. S1 Liao s 2lj s 3lj. · Liao SN = S.
(1 toxic I, J toxic K, I, 6J)
Then, S n, s z, ..., s n is a division of S. It is equivalent to the N elements A n. · A n release K
In a non-labeled box, there is no one box empty. Try to determine N elements A1 ... AN release K non-labeled
The division S (n, k) of the box.
analysis:
The N-element A n ... a n, the number of K a maribaby box is S (n, k). In the configuration process, there are two kinds of mutual
Incompatible situation:
1. Setting (a n) is a subset of K subsets, so it is divided into K-1 subset of K-1 subsets (N-one).
1, K-1) a division;
2. If (AN) is not one of the K subsets, that is, AN must form a subset with other elements. First of all
(A, ..., AN-1) is divided into K subsets, which has a common S (n 1, k). Then add AN to K
A subset of a subset of a subset, this is a way to join. For each joining method, the collection is divided into K
A subset, thus known by the principle of multiplication, and the number of divisions
K * S (n 1, k).
Application of additional principles have been divided into K subsets:
s (n, k) = s (n 1, k 1) ten k * s (n 1, k) (n> 1, k> = 1)
Now consider the boundary conditions of S (N, K): 1). S (n, 0) = 0; when k> n is: s (n, k) = 0;
2) .S (n, 1) = 1; s (n, n) = 1;
This is obtained to get a recursive definition:
s (n, k) = s (n 1, k 1) ten k * s (n 1, k) (n> k, k> = 1)
s (n, k) = O (n S (n, k) = 1 (k = 1) or (k = n) answer: #include Class 22 so-called grants strategy: the original problem is divided into a child problem similar to the problem and the original problem. Recursively explain these subproises, then combine their results to obtain the solution. n = 2 Differentiation method is also known as programs. The level of grasping is three steps in each layer: Decomposition: Decompose the original problem into a series of sub-problems; resolution: Recursively explain each subprocess. If the problem is small enough, the combination is directly solved: the results of the sub-problem are combined into the original problem; [Example 1i combined sort sequence A [1], A [2], ..., a [n] for combining. Sort. Algorithm Analysis: The algorithm of combining sorting is the second method. Decomposition: Subsequently solving N elements contains RN / 2.1 (or LN / 2J) elements: Sort two subsequences with combined sorting method; combined: combine two sorted subsets to obtain Sorting results. The function MERGE (INT P, INT Q, INT R) and MERGE (INT P, INT R) used in front of the greedy method are typical. It is no longer repeated here. Enumeration Method Page 31 so-called enumeration method refers to the inspection conditions given by one by one of the collections of possible solutions, and test conditions given by topics It is useful to determine which use is useless. It can make the sense of life, which is it. In general, if the number of questions determined in advance and the value domain of each solution. We can use ROR language The sentence and condition judgment statement gradually solves or prove. If we cannot predetermine the number of solutions or the value domains of each solution, we can only use the algorithm for implicit graph search, such as backtracking. Due to the backtracking search, the enumeration of each possible solution is generally more than once, so the backtracking method is more expensive than the enumeration method under the same inspection conditions. [Example 0 Fill in the operator Input Any 5 numbers X1, X2, X3, X4, and X5 fill in an operator in each adjacent number of adjacent numbers. After filling the four operators, the expression value is a specified value y (Y is input by the keyboard). Ask all expressions that meet the conditions. Analysis: In order to resolve the priority problem of the operation, we set the following variables: f - subtract marker. When the subtraction is calculated, set f = one 1, otherwise f = 1; Q - If the current operator is ten (1), the left value of the q storage operator; if the current operator is * (/), q Storage two numbers after multiplication (division); P - accumulator. Each fills a operator P = P 10 f * q. Then four for cycles and solve the problem. This is a typical enumeration question, the amount of operation is large. The specific program is still relatively simple, so it is no longer converted. Enumeration method has its own shortcomings, but if you can eliminate those elements that are significantly unsolicted, use enumeration methods in local places, the effect is very significant. [Example 2] Timeup problem has 9 clocks in the 3 × 3 array shown in Figure 1.5.1, and our goal is to rotate the clock pointer to make all the clock pointers point 12 points. There are 9 methods of allowing the rotating clock pointer, each of which is represented by a digital number (1, 2, ..., 9). Figure 1.5-2 shows 9 digital and correspondingly controlled clocks, which are marked with gray in the figure, and their pointer rotates 90 degrees clockwise. Enter data: Enter 9 data, these digital gave the initial position of 9 clocks. The correspondence between the digital and the moment is 0 -> 12 points, 1-> 3 points, 2 -> 6 points, 3 -> 9 points.