[Algorithm 15] full string Arrangement

Source: Internet
Author: User

Topic: enter a string to print all the arrays of the string. For example, input string ABC and output all columns as ABC, ACB, Bac, BCA, cab, and CBA.

 

[Thinking path] let's think about it. If we don't do programming or do it manually, We will basically consider: first fix a letter each time and then arrange the remaining letters in full; then change to a fixed letter, and then arrange the rest of the letters in full. This is just a loop. In other words: assume that the string with a length of N is arranged in the F (n) mode, then our basic idea is to make one of the N letters start each time ", the other n-1 letters are arranged in the F (n-1) format. In this way, we can see that this is obviously a recursive idea.

What is the most important condition for recursion? Or, what is the minimum subproblem? The above ABC example shows that the first step is to fix a and then arrange BC; then arrange BC as a sub-problem, which is also fixed as B and C; then arrange C as a sub-problem, and fix C, there is one character string terminator ('\ 0'); when it comes to the Terminator, all the preceding characters are arranged, so we only need to print all the previous fixed characters.

Based on this idea, we can easily get the followingCode:

 1 # Include <iostream>
2 # Include < String >
3 Using Namespace STD;
4
5 Void Stringpermutation ( Char * STR, Char * Perbegin)
6 {
7 If (STR = NULL | perbegin = NULL)
8 Return ;
9
10 // If the start position is the ending character, print all characters before the ending character
11 If (* Perbegin = ' \ 0 ' )
12 {
13 Cout <STR <Endl;
14 }
15 Else
16 {
17
18 For ( Char * PCH = perbegin; * PCH! = ' \ 0 ' ; ++ PCH)
19 {
20 // Place where the nth character and the first character are exchanged
21 Char Temp = * PCH;
22 * PCH = * perbegin;
23 * Perbegin = temp;
24
25 // Sort all characters after the first character
26 Stringpermutation (STR, perbegin + 1 );
27
28 // Exchange the nth character with the first character
29 Temp = * PCH;
30 * PCH = * perbegin;
31 * Perbegin = temp;
32 }
33 }
34 }
35
36 Void Stringpermutation ( Char * String )
37 {
38 Stringpermutation ( String , String );
39 }
40
41 Int Main ()
42 {
43 Char P [] = " ABC " ;
44
45 Stringpermutation (P );
46
47 Return 0 ;
48 }

The running result is as follows:

[Idea 2] Of course, if we know the library function next_permutation of C ++, we don't have to worry about it because the Library FunctionAlgorithmThey are all encapsulated, so there is nothing to say, just go to the Code:

1 # Include <iostream>
2 # Include < String >
3 Using Namespace STD;
4
5 Void Stringpermutation ( Char * STR, Char * Perbegin)
6 {
7 If (STR = NULL | perbegin = NULL)
8 Return ;
9
10 // If the start position is the ending character, print all characters before the ending character
11 If (* Perbegin = ' \ 0 ' )
12 {
13 Cout <STR <Endl;
14 }
15 Else
16 {
17
18 For ( Char * PCH = perbegin; * PCH! = ' \ 0 ' ; ++ PCH)
19 {
20 // Place where the nth character and the first character are exchanged
21 Char Temp = * PCH;
22 * PCH = * perbegin;
23 * Perbegin = temp;
24
25 // Sort all characters after the first character
26 Stringpermutation (STR, perbegin + 1 );
27
28 // Exchange the nth character with the first character
29 Temp = * PCH;
30 * PCH = * perbegin;
31 * Perbegin = temp;
32 }
33 }
34 }
35
36 Void Stringpermutation ( Char * String )
37 {
38 Stringpermutation ( String , String );
39 }
40
41 Int Main ()
42 {
43 Char P [] = " ABC " ;
44
45 Stringpermutation (P );
46
47 Return 0 ;
48 }

The running result is as follows:

 

References:

[1] He Haitao blog: http://zhedahht.blog.163.com/blog/static/254111742007499363479/

[2] Full arrangement of standard library: http://hi.baidu.com/%D6%D5%BD%E1%D5%DF192/blog/item/a8ce8d23753d1eb64723e800.html

Note:

1) Compile all the code environments in this blogWin7 + vc6. All code has been debugged by the blogger.

2) BloggerPython27This blogArticleCopyright. For Network reprinting, please indicate the sourceHttp://www.cnblogs.com/python27/. You have any suggestions for solving the problem. Please feel free to comment on them.

 

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.