Permutation of strings [algorithm]__ algorithm

Source: Internet
Author: User
Tags php online php online test

transferred from: http://zhedahht.blog.163.com/blog/static/254111742007499363479/

Author: Huang
originating from: http://zhedahht.blog.163.com/

Title: Enter a string to print out all the permutations of the characters in the string. For example, the input string abc, the output of the characters A, B, C can be arranged out of all the strings of ABC, ACB, BAC, BCA, Cab and CBA.

Analysis: This is a good test of recursive understanding of the programming problem, so in the past year has been frequent in the major companies in the interview, written questions.

We use the three-character ABC example to analyze the process of finding string permutations. First we fix the first character a, and we ask for the two-character BC arrangement. When the two-character BC arrangement is made, we swap the first character A with the second B to get the BAC, and then we fix the first character B and then the two-character AC arrangement. Now it's time to put C in the first place. Remember that we have previously exchanged the first character A and the second B, in order to ensure that this time C is still in the first place with the A exchange, we have C and the first character exchange before the first to Exchange B and a back. After exchanging B and a, take C and a in the first position to exchange, get CBA. We fixed the first character C again, and we asked for the arrangement of the next two characters, B, a.

Now that we know how to ask for three-character permutations, it is a typical recursive idea to ask for the next two-character arrangement after the first character is fixed.

Based on the previous analysis, we can get the following reference code:

void permutation (char* pStr, char* pbegin);

/////////////////////////////////////////////////////////////////////////
Get the permutation of a string,
For example, input string abc, it permutation is
ABC ACB BAC BCA CBA cab
/////////////////////////////////////////////////////////////////////////
void permutation (char* pStr)
{
Permutation (pStr, pStr);
}

/////////////////////////////////////////////////////////////////////////
Print the permutation of a string,
Input:pstr-input string
Pbegin-points to the begin char of string
Which we want to permutate in this recursion
/////////////////////////////////////////////////////////////////////////
void permutation (char* pStr, char* pbegin)
{
if (!pstr | |!pbegin)
Return

If Pbegin points to the end of string,
This round of permutation is finished,
Print the permuted string
if (*pbegin = = ' + ')
{
printf ("%s\n", pStr);
}
Otherwise, permute string
Else
{
for (char* pch = Pbegin; *pch! = '); + pch)
{
Swap pCh and Pbegin
char temp = *pch;
*pch = *pbegin;
*pbegin = temp;

Permutation (pStr, Pbegin + 1);

Restore PCh and Pbegin
temp = *pch;
*pch = *pbegin;
*pbegin = temp;
}
}
}

Extension 1: What if you are not all permutations of a character, but rather all combinations of characters? When the input string contains the same string, the same character exchange position is arranged in a different arrangement, but the same combination. For example, if you enter ABC, its combination is a, B, C, AB, AC, BC, ABC.

Extension 2: Enter an array of 8 numbers to determine if it is possible to place the 8 numbers on the 8 vertices of the cube, so that the three vertices on the opposite sides of the 4 group are equal to each other on the cube.

This article has been included in the "sword refers to offer--name Enterprise interviewer explaining typical programming problem" a book, there are changes, the analysis of the book in more detail. Welcome attention.

The subject has been nine degrees online judge system included, welcome readers to http://ac.jobdu.com/hhtproblems.php online test their own code.

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.