Print all characters in a given string

Source: Internet
Author: User

Question:

Enter a string to print all characters in the string. For example, if the input string ABC is used, all the strings ABC, ACB, Bac, BCA, cab, and CBA that can be arranged by characters A, B, and C are output.

 

Solution:

For simplicity, the string does not contain the same characters.

In fact, this is a recursive process: For STR strings, first catch each of them, such as S, and kill it from Str, S + Tr (the next time t is switched to t + SR), and then for the remaining, then, from the rest to the back of the left (the imaginary ones are all on the left, and the rest are all on the right), until the right side is exhausted. The algorithm is as follows:

Void show (strl, strr) {If (strr is not a Null String) {for (for every character in strr) {kick this character to the end of strl recursive show ()}} else {Show string on the left }}

The Code is as follows:

1 # include <iostream> 2 # include <string> 3 4 Using STD: cout; 5 Using STD: Endl; 6 Using STD: CIN; 7 Using STD :: string; 8 9 // in this process, the string is divided into two halves, and the character on the left is fixed, the right side can be combined for various situations. 10 // Finally, the fixed left side can be connected to various situations on the right, that is, the complete strings are 11 void show (string strl, string strr) 12 {13 if (! Strr. empty () // the right side of the non-empty 14 {15 for (INT I = 0; I <strr. size (); I ++) // for each case 16 {17 // be sure to use a copy of the left and right strings 18 string Templ = strl; 19 string tempr = strr; 20 21 Templ. append (1, strr [I]); // Add that character to the back of the string on the left 22 tempr. erase (I, 1); // Delete the 23 show (Templ, tempr) from the string on the right ); // recursively perform 24} 25} 26 else27 {28 cout <strl <Endl; 29} 30} 31 32 int main (void) 33 {34 string left; 35 string test ("ABCDE"); 36 37 show (left, test); 38 cout <"-------------------------------------- \ n" <left <Endl <test; 39 40 cin. get (); 41}

Result:

 

Summary:

Recursive thinking.

In fact, it can be seen as a tree where each branch is split from the root node. The final height to the leaf node is N, and N is the string length. Each leaf node is a situation.

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.