100 Questions selected by Programmers (59)-Combination of strings [algorithm]_ sword refers to an offer

Source: Internet
Author: User
Title: Enter a string that prints all the combinations of characters in the string. For example, if you enter ABC, it has a combination of a, B, C, AB, AC, BC, ABC.

Analysis: In the 28th question of the series "string arrangement", we discussed in detail how to use recursive ideas to find the arrangement of strings. Similarly, the subject can also use recursive ideas to find the combination of strings.

Suppose we want to find a combination of M characters in a string of length n. Let's start by scanning the first character of the string. For the first character, we have two options: one is to put the character in a group, then we need to select the m-1 character in the remaining n-1, instead of putting the character in the group, we need to select the M character in the remaining n-1 characters. Both of these options are easy to implement with recursion. Here is the reference code for this idea:

void combination (char* string)
{
    if (string = = NULL) return
        ;
 
    int length = strlen (string);
    vector<char> result;
    for (int i = 1; I <= length; + + i)
    {
        combination (string, I, result)
    ;
}
 
void combination (char* string, int number, vector<char>& result)
{
    if (number = = 0)
    {
        Vector<char>::iterator iter = Result.begin ();
        for (; Iter < result.end (); + + iter)
            printf ("%c", *iter);
        printf ("\ n");
 
        return;
    }
 
    if (*string = = ' ") return
        ;
 
    Result.push_back (*string);
    Combination (string + 1, number-1, result);
    Result.pop_back ();
 
    Combination (string + 1, number, result);
}


Since a combination can be a combination of 1 characters, a character of 2 characters ... All the time to a combination of n characters, so in the function void combination (char* string) We need a For loop. In addition, we have a vector that holds the characters chosen to put into the combination.

Bo Master He Haitao to this blog article enjoy copyright. The network reprint please indicate the source http://zhedahht.blog.163.com/. Please contact the author to organize your publication. Have any suggestion to solve the problem, welcome to inform in the comment, or add me micro bo Http://weibo.com/zhedahht or http://t.163.com/zhedahht discuss with me. Thank you.

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.