For full sorting, for example, if there are 5 Characters abcde, there are 5! = 120 methods.
First, the mathematical recursion formula is analyzed, and the characters in the abcde string are arranged in full.
Assume that abcde is an input parameter and the output value is a fully sorted set. We can have:
F (abcde) = a + f (bcde)// Note that the plus sign here is not a simple plus sign, but another calculation rule, which will be discussed below.
F (bcde) = B + f (cde)
F (cde) = c + f (de)
F (de) = {de, ed}
The above is the recursive function of the operation, whereThe f () function returns a set, and the plus sign here defines its behavior as inserting a character into a string in order.
For example:
A + bcde inserts a between each position and obtains the following set:
Abcde, bacde, bcade, bcdae, bcdea
Above is a + operation on a single item.
A + f (bcde) means that a performs operations on a set of f (bcde), meaning that a performs + operations on each image in the set.
In the above example, a will generate a set of five items for bcde operations. In fact, the full bcde arrangement can be inferred from the middle school's mathematical calculations! It is 24, and f (bcde) has 24 items. Therefore, a + f (bcde) means that a performs operations on each of the 24 items, and each operation generates 5 items, therefore, a set of 5x24 = 120 items is generated in total. Speaking of this, you should understand the definition of + operation.
In fact, the write + operation is just for the convenience of appearance, or you can replace the table method, for example, the C operation, then the above recursive formula can also be written,
F (abcde) = C (a, f (bcde ))
F (bcde) = C (B, f (cde ))
F (cde) = C (c, f (de ))
F (de) = {de, ed}
The two expressions have the same mathematical meaning.
With a mathematical expression, you can write code.
C # implementation.
- Using System;
- Using System. Collections. Generic;
- Using System. Linq;
- Using System. Text;
-
- Namespace quanpailie
- {
- Class Program
- {
- Static List <string> alllist = new List <string> ();
- Static void Main (string [] args)
- {
- Alllist = f ("abcd ");
- Foreach (var I in alllist)
- {
- Console. WriteLine (I );
- }
- }
-
- Static List <string> f (string instring) // recursive method implementation
- {
- If (instring. Length = 2) // only when there are only 2 Characters left, results can be returned.
- {
- List <string> tmplist = new List <string> ();
- Tmplist. Add (instring [0]. ToString () + instring [1]. ToString ());
- Tmplist. Add (instring [1]. ToString () + instring [0]. ToString ());
- Return tmplist;
- }
- Else
- {
- // If the number is greater than 2 characters, the + operation can only be used to separate the computation, that is, the combine operation implementation method.
- Return combine (instring [0]. toString (), f (instring. remove (0, 1); // separate the first character of instring and complete the combine operation with the subsequent characters. A collection is returned.
- }
- }
-
- Static List <string> combine (string c, List <string> lists) // This is the implementation method of the + operation mentioned above, or the implementation method of the C operation. A set is returned.
- {
- List <string> output = new List <string> ();
- Foreach (string s in lists)
- {
- For (int I = 0; I & lt; = s. Length; I ++) // c is inserted to every position in s.
- {
- String tmps = "";
- Tmps = s. Insert (I, c );
- Output. Add (tmps );
- }
- }
- Return output;
- }
- }
- }
This article is from the "one blog" blog, please be sure to keep this source http://cnn237111.blog.51cto.com/2359144/832060