字符串的排列

<!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimSun; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:黑体; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimHei; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:1 135135232 16 0 262144 0;} @font-face {font-family:"/@黑体"; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:1 135135232 16 0 262144 0;} @font-face {font-family:"/@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; mso-pagination:none; font-size:10.5pt; mso-bidi-font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:宋体; mso-font-kerning:1.0pt;} h2 {mso-style-link:" Char Char"; mso-style-next:正文; margin-top:13.0pt; margin-right:0cm; margin-bottom:13.0pt; margin-left:0cm; text-align:justify; text-justify:inter-ideograph; line-height:173%; mso-pagination:lines-together; page-break-after:avoid; mso-outline-level:2; font-size:16.0pt; font-family:Arial; mso-fareast-font-family:黑体; mso-bidi-font-family:"Times New Roman"; mso-font-kerning:1.0pt;} span.CharChar {mso-style-name:" Char Char"; mso-style-locked:yes; mso-style-link:"标题 2"; mso-ansi-font-size:16.0pt; mso-bidi-font-size:16.0pt; font-family:Arial; mso-ascii-font-family:Arial; mso-fareast-font-family:黑体; mso-hansi-font-family:Arial; mso-font-kerning:1.0pt; mso-ansi-language:EN-US; mso-fareast-language:ZH-CN; mso-bidi-language:AR-SA; font-weight:bold;} /* Page Definitions */ @page {mso-page-border-surround-header:no; mso-page-border-surround-footer:no;} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt; mso-header-margin:36.0pt; mso-footer-margin:36.0pt; mso-paper-source:0;} div.Section1 {page:Section1;} -->

字符串的排列

题目:输入一个字符串,打印出该字符串中字符的所有排列。例如输入字符串 abc ,则输出由字符 a b c 所能排列出来的所有字符串 abc acb bac bca cab cba

分析:这是一道很好的考查对递归理解的编程题,因此在过去一年中频繁出现在各大公司的面试、笔试题中。

我们以三个字符 abc 为例来分析一下求字符串排列的过程。首先我们固定第一个字符 a ,求后面两个字符 bc 的排列。当两个字符 bc 的排列求好之后,我们把第一个字符 a 和后面的 b 交换,得到 bac ,接着我们固定第一个字符 b ,求后面两个字符 ac 的排列。现在是把 c 放到第一位置的时候了。记住前面我们已经把原先的第一个字符 a 和后面的 b 做了交换,为了保证这次 c 仍然是和原先处在第一位置的 a 交换,我们在拿 c 和第一个字符交换之前,先要把 b a 交换回来。在交换 b a 之后,再拿 c 和处在第一位置的 a 进行交换,得到 cba 。我们再次固定第一个字符 c ,求后面两个字符 b a 的排列。

既然我们已经知道怎么求三个字符的排列,那么固定第一个字符之后求后面两个字符的排列,就是典型的递归思路了。

基于前面的分析,我们可以得到如下的参考代码:

void Permutation(char * pStr, char * pBegin);

/
// Get the permutation of a string,
// for example, input string abc, its 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 == '/0' )
    {
       printf("%s/n" , pStr);
    }
    // otherwise, permute string
    else
    {
       for (char * pCh = pBegin; *pCh != '/0' ; ++ 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;
       }
    }
}

扩展 1 :如果不是求字符的所有排列,而是求字符的所有组合,应该怎么办呢?当输入的字符串中含有相同的字符串时,相同的字符交换位置是不同的排列,但是同一个组合。举个例子,如果输入 aaa ,那么它的排列是 6 aaa ,但对应的组合只有一个。

扩展 2 :输入一个含有 8 个数字的数组,判断有没有可能把这 8 个数字分别放到正方体的 8 个顶点上,使得正方体上三组相对的面上的 4 个顶点的和相等。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值