1.字符串的全排列。
全排列就是从第一个数起每个数分别与它后面的数交换。
递归实现
从集合中依次选出每一个元素,作为排列的第一个元素,然后对剩余的元素进行全排列,如此递归处理,从而得到所有元素的全排列。以对字符串abc进行全排列为例,我们可以这么做:以abc为例
固定a,求后面bc的排列:abc,acb,求好后,a和b交换,得到bac
固定b,求后面ac的排列:bac,bca,求好后,a和c交换,得到cba
固定c,求后面ba的排列:cba,cab。代码可如下编写所示:
template
void CalcAllPermutation_R(T perm[], int first, int num)
{
if (num <= 1) {
return;
}
for (int i = first; i < first + num; ++i) {
swap(perm[i], perm[first]);
CalcAllPermutation_R(perm, first + 1, num - 1);
swap(perm[i], perm[first]);
}
}
或者这样:
#include
using namespace std;
#include
void Permutation(char* pStr, char* pBegin)
{
assert(pStr && pBegin);
if(*pBegin == '\0')
printf("%s\n",pStr);
else
{
for(char* pCh = pBegin; *pCh != '\0'; pCh++)
{
swap(*pBegin,*pCh);
Permutation(pStr, pBegin+1);
swap(*pBegin,*pCh);
}
}
}
int main(void)
{
char str[] = "abc";
Permutation(str,str);
return 0;
}
template
void CalcAllPermutation(T perm[], int num)
{
if (num < 1)
return;
while (true) {
int i;
for (i = num - 2; i >= 0; --i) {
if (perm[i] < perm[i + 1])
break;
}
if (i < 0)
break; // 已经找到所有排列
int k;
for (k = num - 1; k > i; --k) {
if (perm[k] > perm[i])
break;
}
swap(perm[i], perm[k]);
reverse(perm + i + 1, perm + num);
}
}
去重的全排列就是从第一个数字起每个数分别与它后面非重复出现的数字交换。
#include
using namespace std;
#include
//在[nBegin,nEnd)区间中是否有字符与下标为pEnd的字符相等
bool IsSwap(char* pBegin , char* pEnd)
{
char *p;
for(p = pBegin ; p < pEnd ; p++)
{
if(*p == *pEnd)
return false;
}
return true;
}
void Permutation(char* pStr , char *pBegin)
{
assert(pStr);
if(*pBegin == '\0')
{
static int num = 1; //局部静态变量,用来统计全排列的个数
printf("第%d个排列\t%s\n",num++,pStr);
}
else
{
for(char *pCh = pBegin; *pCh != '\0'; pCh++) //第pBegin个数分别与它后面的数字交换就能得到新的排列
{
if(IsSwap(pBegin , pCh))
{
swap(*pBegin , *pCh);
Permutation(pStr , pBegin + 1);
swap(*pBegin , *pCh);
}
}
}
}
int main(void)
{
char str[] = "baa";
Permutation(str , str);
return 0;
}
假设我们想在长度为n的字符串中求m个字符的组合。我们先从头扫描字符串的第一个字符。针对第一个字符,我们有两种选择:第一是把这个字符放到组合中去,接下来我们需要在剩下的n-1个字符中选取m-1个字符;第二是不把这个字符放到组合中去,接下来我们需要在剩下的n-1个字符中选择m个字符。这两种选择都很容易用递归实现。下面是这种思路的参考代码:
#include
#include
#include
using namespace std;
#include
void Combination(char *string ,int number,vector &result);
void Combination(char *string)
{
assert(string != NULL);
vector result;
int i , length = strlen(string);
for(i = 1 ; i <= length ; ++i)
Combination(string , i ,result);
}
void Combination(char *string ,int number , vector &result)
{
assert(string != NULL);
if(number == 0)
{
static int num = 1;
printf("第%d个组合\t",num++);
vector::iterator iter = result.begin();
for( ; iter != result.end() ; ++iter)
printf("%c",*iter);
printf("\n");
return ;
}
if(*string == '\0')
return ;
result.push_back(*string);
Combination(string + 1 , number - 1 , result);
result.pop_back();
Combination(string + 1 , number , result);
}
int main(void)
{
char str[] = "abc";
Combination(str);
return 0;
}
或者这样
#include
using namespace std;
int a[] = {1,3,5,4,6};
char str[] = "abcde";
void print_subset(int n , int s)
{
printf("{");
for(int i = 0 ; i < n ; ++i)
{
if( s&(1<
由于八个皇后的任意两个不能处在同一行,那么这肯定是每一个皇后占据一行。于是我们可以定义一个数组ColumnIndex[8],数组中第i个数字表示位于第i行的皇后的列号。先把ColumnIndex的八个数字分别用0-7初始化,接下来我们要做的事情就是对数组ColumnIndex做全排列。由于我们是用不同的数字初始化数组中的数字,因此任意两个皇后肯定不同列。我们只需要判断得到的每一个排列对应的八个皇后是不是在同一对角斜线上,也就是数组的两个下标i和j,是不是i-j==ColumnIndex[i]-Column[j]或者j-i==ColumnIndex[i]-ColumnIndex[j]。
#include
using namespace std;
int g_number = 0;
void Permutation(int * , int , int );
void Print(int * , int );
void EightQueen( )
{
const int queens = 8;
int ColumnIndex[queens];
for(int i = 0 ; i < queens ; ++i)
ColumnIndex[i] = i; //初始化
Permutation(ColumnIndex , queens , 0);
}
bool Check(int ColumnIndex[] , int length)
{
int i,j;
for(i = 0 ; i < length; ++i)
{
for(j = i + 1 ; j < length; ++j)
{
if( i - j == ColumnIndex[i] - ColumnIndex[j] || j - i == ColumnIndex[i] - ColumnIndex[j]) //在正、副对角线上
return false;
}
}
return true;
}
void Permutation(int ColumnIndex[] , int length , int index)
{
if(index == length)
{
if( Check(ColumnIndex , length) ) //检测棋盘当前的状态是否合法
{
++g_number;
Print(ColumnIndex , length);
}
}
else
{
for(int i = index ; i < length; ++i) //全排列
{
swap(ColumnIndex[index] , ColumnIndex[i]);
Permutation(ColumnIndex , length , index + 1);
swap(ColumnIndex[index] , ColumnIndex[i]);
}
}
}
void Print(int ColumnIndex[] , int length)
{
printf("%d\n",g_number);
for(int i = 0 ; i < length; ++i)
printf("%d ",ColumnIndex[i]);
printf("\n");
}
int main(void)
{
EightQueen();
return 0;
}
#include
#include
using namespace std;
list list1;
void find_factor(int sum,int n)
{
//递归出口
if(n<=0||sum<=0)
return;
//输出找到的数
if(sum==n)
{
list1.reverse();
for(list::iterator iter=list1.begin();iter!=list1.end();iter++)
cout<<*iter<<"+";
cout<>sum>>n;
cout<<"所有可能的序列,如下:"<