zoukankan      html  css  js  c++  java
  • 转:字符串的全排列和组合算法

    全排列在笔试面试中很热门,因为它难度适中,既可 以考察递归实现,又能进一步考察非递归的实现,便于区分出考生的水平。所以在百度和迅雷的校园招聘以及程序员和软件设计师的考试中都考到了,因此本文对全 排列作下总结帮助大家更好的学习和理解。对本文有任何补充之处,欢迎大家指出。
    首先来看看题目是如何要求的(百度迅雷校招笔试题)。
    一、字符串的排列
    用C++写一个函数, 如 Foo(const char *str), 打印出 str 的全排列,
    如 abc 的全排列: abc, acb, bca, dac, cab, cba

    一、全排列的递归实现

    为方便起见,用123来示例下。123的全 排列有123、132、213、231、312、321这六种。首先考虑213和321这二个数是如何得出的。显然这二个都是123中的1与后面两数交换 得到的。然后可以将123的第二个数和每三个数交换得到132。同理可以根据213和321来得231和312。因此可以知道——全排列就是从第一个数字 起每个数分别与它后面的数字交换。找到这个规律后,递归的代码就很容易写出来了:

    1. #include<iostream>  
    2. using namespace std;  
    3. #include<assert.h>  
    4.   
    5. void Permutation(char* pStr, char* pBegin)  
    6. {  
    7.     assert(pStr && pBegin);  
    8.   
    9.     if(*pBegin == '')  
    10.         printf("%s ",pStr);  
    11.     else  
    12.     {  
    13.         for(char* pCh = pBegin; *pCh != ''; pCh++)  
    14.         {  
    15.             swap(*pBegin,*pCh);  
    16.             Permutation(pStr, pBegin+1);  
    17.             swap(*pBegin,*pCh);  
    18.         }  
    19.     }  
    20. }  
    21.   
    22. int main(void)  
    23. {  
    24.     char str[] = "abc";  
    25.     Permutation(str,str);  
    26.     return 0;  
    27. }  

    另外一种写法:

    1. //k表示当前选取到第几个数,m表示共有多少个数  
    2. void Permutation(char* pStr,int k,int m)  
    3. {  
    4.     assert(pStr);  
    5.   
    6.     if(k == m)  
    7.     {  
    8.         static int num = 1;  //局部静态变量,用来统计全排列的个数  
    9.         printf("第%d个排列 %s ",num++,pStr);  
    10.     }  
    11.     else  
    12.     {  
    13.         for(int i = k; i <= m; i++)  
    14.         {  
    15.             swap(*(pStr+k),*(pStr+i));  
    16.             Permutation(pStr, k + 1 , m);  
    17.             swap(*(pStr+k),*(pStr+i));  
    18.         }  
    19.     }  
    20. }  
    21.   
    22. int main(void)  
    23. {  
    24.     char str[] = "abc";  
    25.     Permutation(str , 0 , strlen(str)-1);  
    26.     return 0;  
    27. }  

    如果字符串中有重复字符的话,上面的那个方法肯定不会符合要求的,因此现在要想办法来去掉重复的数列。
    二、去掉重复的全排列的递归实现
    由于全排列就是从第一个数字起每个数分别与它后面的数字交换。我们先尝试加个这样的判断——如果一个数与后面的数字相同那么这二个数就不交换了。如 122,第一个数与后面交换得212、221。然后122中第二数就不用与第三个数交换了,但对212,它第二个数与第三个数是不相同的,交换之后得到 221。与由122中第一个数与第三个数交换所得的221重复了。所以这个方法不行。

    换种思维,对122,第一个数1与第二个数2交换得到212,然后考虑第一个数1与第三个数2交换,此时由于第三个数等于第二个数,所以第一个数不再与第三个数交换。再考虑212,它的第二个数与第三个数交换可以得到解决221。此时全排列生成完毕。
    这样我们也得到了在全排列中去掉重复的规则——去重的全排列就是从第一个数字起每个数分别与它后面非重复出现的数字交换。下面给出完整代码:

    1. #include<iostream>  
    2. using namespace std;  
    3. #include<assert.h>  
    4.   
    5. //在[nBegin,nEnd)区间中是否有字符与下标为pEnd的字符相等  
    6. bool IsSwap(char* pBegin , char* pEnd)  
    7. {  
    8.     char *p;  
    9.     for(p = pBegin ; p < pEnd ; p++)  
    10.     {  
    11.         if(*p == *pEnd)  
    12.             return false;  
    13.     }  
    14.     return true;  
    15. }  
    16. void Permutation(char* pStr , char *pBegin)  
    17. {  
    18.     assert(pStr);  
    19.   
    20.     if(*pBegin == '')  
    21.     {  
    22.         static int num = 1;  //局部静态变量,用来统计全排列的个数  
    23.         printf("第%d个排列 %s ",num++,pStr);  
    24.     }  
    25.     else  
    26.     {  
    27.         for(char *pCh = pBegin; *pCh != ''; pCh++)   //第pBegin个数分别与它后面的数字交换就能得到新的排列     
    28.         {  
    29.             if(IsSwap(pBegin , pCh))  
    30.             {  
    31.                 swap(*pBegin , *pCh);  
    32.                 Permutation(pStr , pBegin + 1);  
    33.                 swap(*pBegin , *pCh);  
    34.             }  
    35.         }  
    36.     }  
    37. }  
    38.   
    39. int main(void)  
    40. {  
    41.     char str[] = "baa";  
    42.     Permutation(str , str);  
    43.     return 0;  
    44. }  

    OK,到现在我们已经能熟练写出递归的方法了,并且考虑了字符串中的重复数据可能引发的重复数列问题。那么如何使用非递归的方法来得到全排列了?

    三、全排列的非递归实现
    要考虑全排列的非递归实现,先来考虑如何计算字符串的下一个排列。如"1234"的下一个排列就是"1243"。只要对字符串反复求出下一个排列,全排列的也就迎刃而解了。
    如何计算字符串的下一个排列了?来考虑"926520"这个字符串,我们从后向前找第一双相邻的递增数字,"20"、"52"都是非递增的,"26 "即满足要求,称前一个数字2为替换数,替换数的下标称为替换点,再从后面找一个比替换数大的最小数(这个数必然存在),0、2都不行,5可以,将5和2 交换得到"956220",然后再将替换点后的字符串"6220"颠倒即得到"950226"。
    对于像“4321”这种已经是最“大”的排列,采用STL中的处理方法,将字符串整个颠倒得到最“小”的排列"1234"并返回false。

    这样,只要一个循环再加上计算字符串下一个 排列的函数就可以轻松的实现非递归的全排列算法。按上面思路并参考STL中的实现源码,不难写成一份质量较高的代码。值得注意的是在循环前要对字符串排序 下,可以自己写快速排序的代码(请参阅《白话经典算法之六 快速排序 快速搞定》),也可以直接使用VC库中的快速排序函数(请参阅《使用VC库函数中的快速排序函数》)。下面列出完整代码:

    1. #include<iostream>  
    2. #include<algorithm>  
    3. #include<cstring>  
    4. using namespace std;  
    5. #include<assert.h>  
    6.   
    7. //反转区间  
    8. void Reverse(char* pBegin , char* pEnd)  
    9. {  
    10.     while(pBegin < pEnd)  
    11.         swap(*pBegin++ , *pEnd--);  
    12. }  
    13. //下一个排列  
    14. bool Next_permutation(char a[])  
    15. {  
    16.     assert(a);  
    17.     char *p , *q , *pFind;  
    18.     char *pEnd = a + strlen(a) - 1;  
    19.     if(a == pEnd)  
    20.         return false;  
    21.     p = pEnd;  
    22.     while(p != a)  
    23.     {  
    24.         q = p;  
    25.         p--;  
    26.         if(*p < *q)  //找降序的相邻2数,前一个数即替换数    
    27.         {  
    28.              //从后向前找比替换点大的第一个数  
    29.             pFind = pEnd;  
    30.             while(*pFind < *p)  
    31.                 --pFind;  
    32.             swap(*p , *pFind);  
    33.             //替换点后的数全部反转  
    34.             Reverse(q , pEnd);  
    35.             return true;  
    36.         }  
    37.     }  
    38.     Reverse(a , pEnd);   //如果没有下一个排列,全部反转后返回false     
    39.     return false;  
    40. }  
    41.   
    42. int cmp(const void *a,const void *b)  
    43. {  
    44.     return int(*(char *)a - *(char *)b);  
    45. }  
    46. int main(void)  
    47. {  
    48.     char str[] = "bac";  
    49.     int num = 1;  
    50.     qsort(str , strlen(str),sizeof(char),cmp);  
    51.     do  
    52.     {  
    53.         printf("第%d个排列 %s ",num++,str);   
    54.     }while(Next_permutation(str));  
    55.     return 0;  
    56. }  

    至此我们已经运用了递归与非递归的方法解决了全排列问题,总结一下就是:
    1、全排列就是从第一个数字起每个数分别与它后面的数字交换。
    2、去重的全排列就是从第一个数字起每个数分别与它后面非重复出现的数字交换。
    3、全排列的非递归就是由后向前找替换数和替换点,然后由后向前找第一个比替换数大的数与替换数交换,最后颠倒替换点后的所有数据。

    二、字符串的组合

    题目:输入一个字符串,输出该字符串中字符的所有组合。举个例子,如果输入abc,它的组合有a、b、c、ab、ac、bc、abc。

    上面我们详细讨论了如何用递归的思路求字符串的排列。同样,本题也可以用递归的思路来求字符串的组合。

    假设我们想在长度为n的字符串中求m个字符的组合。我们先从头扫描字符串的第一个字符。针对第一个字符,我们有两种选择:第一是把这个字符放到组合中去, 接下来我们需要在剩下的n-1个字符中选取m-1个字符;第二是不把这个字符放到组合中去,接下来我们需要在剩下的n-1个字符中选择m个字符。这两种选 择都很容易用递归实现。下面是这种思路的参考代码:

    1. #include<iostream>  
    2. #include<vector>  
    3. #include<cstring>  
    4. using namespace std;  
    5. #include<assert.h>  
    6.   
    7. void Combination(char *string ,int number,vector<char> &result);  
    8.   
    9. void Combination(char *string)  
    10. {  
    11.     assert(string != NULL);  
    12.     vector<char> result;  
    13.     int i , length = strlen(string);  
    14.     for(i = 1 ; i <= length ; ++i)  
    15.         Combination(string , i ,result);  
    16. }  
    17.   
    18. void Combination(char *string ,int number , vector<char> &result)  
    19. {  
    20.     assert(string != NULL);  
    21.     if(number == 0)  
    22.     {  
    23.         static int num = 1;  
    24.         printf("第%d个组合 ",num++);  
    25.   
    26.         vector<char>::iterator iter = result.begin();  
    27.         for( ; iter != result.end() ; ++iter)  
    28.             printf("%c",*iter);  
    29.         printf(" ");  
    30.         return ;  
    31.     }  
    32.     if(*string == '')  
    33.         return ;  
    34.     result.push_back(*string);  
    35.     Combination(string + 1 , number - 1 , result);  
    36.     result.pop_back();  
    37.     Combination(string + 1 , number , result);  
    38. }  
    39.   
    40. int main(void)  
    41. {  
    42.     char str[] = "abc";  
    43.     Combination(str);  
    44.     return 0;  
    45. }  

    由于组合可以是1个字符的组合,2个字符的字符……一直到n个字符的组合,因此在函数void Combination(char* string),我们需要一个for循环。另外,我们用一个vector来存放选择放进组合里的字符。
    方法二:用位运算来实现求组合

    1. #include<iostream>  
    2. using namespace std;  
    3.   
    4. int a[] = {1,3,5,4,6};  
    5. char str[] = "abcde";  
    6.   
    7. void print_subset(int n , int s)  
    8. {  
    9.     printf("{");  
    10.     for(int i = 0 ; i < n ; ++i)  
    11.     {  
    12.         if( s&(1<<i) )         // 判断s的二进制中哪些位为1,即代表取某一位  
    13.             printf("%c ",str[i]);   //或者a[i]  
    14.     }  
    15.     printf("} ");  
    16. }  
    17.   
    18. void subset(int n)  
    19. {  
    20.     for(int i= 0 ; i < (1<<n) ; ++i)  
    21.     {  
    22.         print_subset(n,i);  
    23.     }  
    24. }  
    25.   
    26.   
    27.   
    28. int main(void)  
    29. {  
    30.     subset(5);  
    31.     return 0;  
    32. }  


    字符串全排列扩展----八皇后问题
        题目:在8×8的国际象棋上摆放八个皇后,使其不能相互攻击,即任意两个皇后不得处在同一行、同一列或者同一对角斜线上。下图中的每个黑色格子表示一个皇后,这就是一种符合条件的摆放方法。请求出总共有多少种摆法。


        这就是有名的八皇后问题。解决这个问题通常需要用递归,而递归对编程能力的要求比较高。因此有不少面试官青睐这个题目,用来考察应聘者的分析复杂问题的能力以及编程的能力。

    由于八个皇后的任意两个不能处在同一行,那么这肯定是每一个皇后占据一行。于是我们可以定义一个数组ColumnIndex[8],数组中第i个数字表示 位于第i行的皇后的列号。先把ColumnIndex的八个数字分别用0-7初始化,接下来我们要做的事情就是对数组ColumnIndex做全排列。由 于我们是用不同的数字初始化数组中的数字,因此任意两个皇后肯定不同列。我们只需要判断得到的每一个排列对应的八个皇后是不是在同一对角斜线上,也就是数 组的两个下标i和j,是不是i-j==ColumnIndex[i]-Column[j]或者j-i==ColumnIndex[i]- ColumnIndex[j]。

    关于排列的详细讨论,详见上面的讲解。
    接下来就是写代码了。思路想清楚之后,编码并不是很难的事情。下面是一段参考代码:

    1. #include<iostream>  
    2. using namespace std;  
    3.   
    4. int g_number = 0;  
    5. void Permutation(int * , int  , int );  
    6. void Print(int * , int );  
    7.   
    8. void EightQueen( )  
    9. {  
    10.     const int queens = 8;  
    11.     int ColumnIndex[queens];  
    12.     for(int i = 0 ; i < queens ; ++i)  
    13.         ColumnIndex[i] = i;    //初始化  
    14.     Permutation(ColumnIndex , queens , 0);  
    15. }  
    16.   
    17. bool Check(int ColumnIndex[] , int length)  
    18. {  
    19.     int i,j;  
    20.     for(i = 0 ; i < length; ++i)  
    21.     {  
    22.         for(j = i + 1 ; j < length; ++j)  
    23.         {  
    24.             if( i - j == ColumnIndex[i] - ColumnIndex[j] || j - i == ColumnIndex[i] - ColumnIndex[j])   //在正、副对角线上  
    25.                 return false;  
    26.         }  
    27.     }  
    28.     return true;  
    29. }  
    30. void Permutation(int ColumnIndex[] , int length , int index)  
    31. {  
    32.     if(index == length)  
    33.     {  
    34.         if( Check(ColumnIndex , length) )   //检测棋盘当前的状态是否合法  
    35.         {  
    36.             ++g_number;  
    37.             Print(ColumnIndex , length);  
    38.         }  
    39.     }  
    40.     else  
    41.     {  
    42.         for(int i = index ; i < length; ++i)   //全排列  
    43.         {  
    44.             swap(ColumnIndex[index] , ColumnIndex[i]);  
    45.             Permutation(ColumnIndex , length , index + 1);  
    46.             swap(ColumnIndex[index] , ColumnIndex[i]);  
    47.         }  
    48.     }  
    49. }  
    50.   
    51. void Print(int ColumnIndex[] , int length)  
    52. {  
    53.     printf("%d ",g_number);  
    54.     for(int i = 0 ; i < length; ++i)  
    55.         printf("%d ",ColumnIndex[i]);  
    56.     printf(" ");  
    57. }  
    58.   
    59. int main(void)  
    60. {  
    61.     EightQueen();  
    62.     return 0;  
    63. }  

    转载:http://zhedahht.blog.163.co

    题目:输入两个整数n和m,从数列1,2,3...n中随意取几个数,使其和等于m,要求列出所有的组合。

      1. #include <iostream>  
      2. #include <list>  
      3. using namespace std;  
      4. list<int> list1;  
      5. void find_factor(int sum,int n)  
      6. {  
      7.     //递归出口  
      8.     if(n<=0||sum<=0)  
      9.         return;  
      10.     //输出找到的数  
      11.     if(sum==n)  
      12.     {  
      13.         list1.reverse();  
      14.         for(list<int>::iterator iter=list1.begin();iter!=list1.end();iter++)  
      15.             cout<<*iter<<"+";  
      16.         cout<<n<<endl;  
      17.         list1.reverse();  
      18.     }  
      19.     list1.push_front(n);  
      20.     find_factor(sum-n,n-1);//n放在里面  
      21.     list1.pop_front();  
      22.     find_factor(sum,n-1);//n不放在里面  
      23. }  
      24.   
      25. int main(void)  
      26. {  
      27.     int sum,n;  
      28.     cin>>sum>>n;  
      29.     cout<<"所有可能的序列,如下:"<<endl;  
      30.     find_factor(sum,n);  
      31.     return 0;  
      32. }  
  • 相关阅读:
    spark streaming 概述
    spark sql 的性能调优
    LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal (用中序和后序树遍历来建立二叉树)
    LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal (用先序和中序树遍历来建立二叉树)
    LeetCode 90. Subsets II (子集合之二)
    LeetCode 88. Merge Sorted Array(合并有序数组)
    LeetCode 81. Search in Rotated Sorted Array II(在旋转有序序列中搜索之二)
    LeetCode 80. Remove Duplicates from Sorted Array II (从有序序列里移除重复项之二)
    LeetCode 79. Word Search(单词搜索)
    LeetCode 78. Subsets(子集合)
  • 原文地址:https://www.cnblogs.com/stephen-init/p/4335524.html
Copyright © 2011-2022 走看看