Recursive Implementation of full Arrangement

Source: Internet
Author: User

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.

 

 
 
  1. Using System;
  2. Using System. Collections. Generic;
  3. Using System. Linq;
  4. Using System. Text;
  5.  
  6. Namespace quanpailie
  7. {
  8. Class Program
  9. {
  10. Static List <string> alllist = new List <string> ();
  11. Static void Main (string [] args)
  12. {
  13. Alllist = f ("abcd ");
  14. Foreach (var I in alllist)
  15. {
  16. Console. WriteLine (I );
  17. }
  18. }
  19.  
  20. Static List <string> f (string instring) // recursive method implementation
  21. {
  22. If (instring. Length = 2) // only when there are only 2 Characters left, results can be returned.
  23. {
  24. List <string> tmplist = new List <string> ();
  25. Tmplist. Add (instring [0]. ToString () + instring [1]. ToString ());
  26. Tmplist. Add (instring [1]. ToString () + instring [0]. ToString ());
  27. Return tmplist;
  28. }
  29. Else
  30. {
  31. // 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.
  32. 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.
  33. }
  34. }
  35.  
  36. 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.
  37. {
  38. List <string> output = new List <string> ();
  39. Foreach (string s in lists)
  40. {
  41. For (int I = 0; I & lt; = s. Length; I ++) // c is inserted to every position in s.
  42. {
  43. String tmps = "";
  44. Tmps = s. Insert (I, c );
  45. Output. Add (tmps );
  46. }
  47. }
  48. Return output;
  49. }
  50. }
  51. }

 

This article is from the "one blog" blog, please be sure to keep this source http://cnn237111.blog.51cto.com/2359144/832060

Related Article

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.