递归与字符串

  以前做过类似字符串的题目,当时觉得字符串也能用递归来做非常神奇,后来思考了下,觉得只要抓住字符串是由一个个字符组成的,从第一个字符到最后一个字符每一个都可以作为一次递归,把index作为参数传进去就行,以这种思想为指导才把此题解了出来。 

  对于每个字符只有两种情况,设当前字符串指针为x, 目标字符串指针为y:
  GetString(x,y)= GetString(x+1,y), 不取当前字符
  GetString(x,y) = GetString(x+1,y+1), 取当前字符

  

1 //////////////////////////////////////////////////////////////////////////
2  // 递归实现M个字符选N个字符
3  //
4 // param:
5 // origin: original string
6 // oirIndex: index point to original string
7 // M: length of original string
8 // des: destination string
9 // desIndex: index point to des string
10 // N: length of des string
11 //////////////////////////////////////////////////////////////////////////
12 void GetString(char* origin, int oriIndex, int M, char* des, int desIndex, int N)
13 {
14 if(M < N)
15 return;
16
17 //output
18 if(strlen(des) == N)
19 {
20 cout<<des<<endl;
21 return;
22 }
23 //reach the end of origin or des
24 if(oriIndex == M)
25 return;
26 if(desIndex == N)
27 {
28 return;
29 }
30
31 //pick oriIndex
32 des[desIndex]= origin[oriIndex];
33 GetString(origin,++oriIndex,M,des,++desIndex,N);
34 //not pick
35 des[--desIndex] = '\0';
36 --oriIndex;
37 GetString(origin,++oriIndex,M,des,desIndex,N);
38 }


题目:输入一个字符串,打印出该字符串中字符的所有排列。例如输入字符串abc,则输出由字符abc所能排列出来的所有字符串abcacbbacbcacabcba

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

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

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

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

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:如果不是求字符的所有排列,而是求字符的所有组合,应该怎么办呢?当输入的字符串中含有相同的字符串时,相同的字符交换位置是不同的排列,但是同一个组合。举个例子,如果输入abc,它的组合有a、b、c、ab、ac、bc、abc

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

博主何海涛对本博客文章享有版权。网络转载请注明出处http://zhedahht.blog.163.com/。整理出版物请和作者联系。对解题思路有任何建议,欢迎在评论中告知,或者加我微博http://weibo.com/zhedahht或者http://t.163.com/zhedahht与我讨论。谢谢。

原文地址:https://www.cnblogs.com/mysunnyday/p/2184476.html