全排列打印

全排列打印

全排列的要求:

输入:字符串"abc"。

输出:如下图示,



思路1——全排列的递归实现核心思想

比如对于字符串”abc”,

第一步:求所有可能出现在第一个位置的字符即:a,b,c。

使用方法:把第一个字符和后面的b、c字符进行交换。

第二步:把第一个字符后面的所有字符仍然看成两部分,即后面的第一个字符及除此之外的其他字符。然后完成后面的第一个字符与其他字符的交换。比如:第2个位置的b与第3个位置c的交换。

第三步:依次递归,直到末尾的’\0’为止。

 

全排列的递归实现:                                     

static int g_sCnt= 0;
 
//permutation的重载版本.
voidpermutation(char* pStr, char* pBegin)
{
       if(*pBegin == '\0')
       {
              ++g_sCnt;
              cout << pStr << endl;
       }
       else
       {
              for(char* pCh = pBegin; *pCh != '\0'; ++pCh)
              {
                     //从第一个字符依次和后面的字符进行交换.
                     char temp = *pCh;
                     *pCh = *pBegin;
                     *pBegin = temp;
 
                     permutation(pStr,pBegin+1);
                    
                     //交换回原样,以便再递归处理后面的字符.
                     temp = *pCh;
                     *pCh = *pBegin;
                     *pBegin = temp;
 
              }//end for
       }//end else
}
//全排列处理函数
voidpermutation(char* pStr)
{
    if(pStr== NULL)
        {
              return;
        }
       else
        {
              permutation(pStr,pStr);
        }
}
 
int main()
{
    char strSrc[] = "abcd";
    permutation(strSrc);
    cout<< "共 " << g_sCnt << " 种排列!" <<endl;
        
return 0;
}


思路2——全排列的STL实现:

有时候递归的效率使得我们不得不考虑除此之外的其他实现,很多把递归算法转换到非递归形式的算法是比较难的,这个时候我们不要忘记了标准模板库STL已经实现的那些算法,这让我们非常轻松。

STL有一个函数next_permutation(),它的作用是如果对于一个序列,存在按照字典排序后这个排列的下一个排列,那么就返回true且产生这个排列,否则返回false。

注意,为了产生全排列,这个序列要是有序的,也就是说要调用一次sort。

实现很简单,我们看一下代码:

      

 void permutation(char* str)
{
       int length = strlen(str);
 
       //第1步:排序
    sort(str,str+length);
 
       //第2步:调用函数next_permutation
    do
    {
        for(int i=0; i<length; i++)
              {
                     cout<<str[i];
              }
        cout << endl;
    }while(next_permutation(str,str+length));
   
}
 
int main()
{
    char str[] = "acb";
    permutation(str);
   
    return 0;
}


思路3:全排列的字典树实现


待笔者研究透字典树后深入写出该算法。

好的,我可以给你提供一个 C 语言实现的 Johnson Trotter 算法来生成 1 到 n 的全排列。以下是实现的代码: ```c #include <stdio.h> #include <stdlib.h> // 定义全局变量 int *p; // 存放全排列的数组 int *dir; // 存放每个元素的方向,1 表示向右,-1 表示向左 // 初始化全排列和方向数组 void init(int n) { // 分配内存空间 p = (int *)malloc(n * sizeof(int)); dir = (int *)malloc(n * sizeof(int)); // 初始化全排列为 1, 2, ..., n for (int i = 0; i < n; i++) { p[i] = i + 1; dir[i] = -1; // 初始方向都向左 } } // 打印全排列 void print_permutation(int n, int count) { printf("%d ", count); for (int i = 0; i < n; i++) { printf("%d", p[i]); } printf("\n"); } // 交换数组中的两个元素 void swap(int *a, int *b) { int temp = *a; *a = *b; *b = temp; } // 执行 Johnson Trotter 算法,生成全排列 void johnson_trotter(int n) { int count = 1; // 记录第几个全排列 while (1) { print_permutation(n, count); // 打印全排列 count++; // 全排列计数加 1 // 找到最大的可以移动的元素 int mobile = -1; int mobile_idx = -1; for (int i = 0; i < n; i++) { if ((dir[i] == -1 && i > 0 && p[i] > p[i-1]) || (dir[i] == 1 && i < n-1 && p[i] > p[i+1])) { if (p[i] > mobile) { mobile = p[i]; mobile_idx = i; } } } // 如果没有可以移动的元素,说明全排列已经生成完毕,退出循环 if (mobile_idx == -1) { break; } // 移动最大的可以移动的元素 if (dir[mobile_idx] == -1) { swap(&p[mobile_idx], &p[mobile_idx-1]); swap(&dir[mobile_idx], &dir[mobile_idx-1]); } else { swap(&p[mobile_idx], &p[mobile_idx+1]); swap(&dir[mobile_idx], &dir[mobile_idx+1]); } // 翻转所有比 mobile 大的元素的方向 for (int i = 0; i < n; i++) { if (p[i] > mobile) { dir[i] = -dir[i]; } } } // 释放内存空间 free(p); free(dir); } int main() { int n; scanf("%d", &n); init(n); johnson_trotter(n); return 0; } ``` 以上代码中,我们定义了一个 `init` 函数来初始化全排列和方向数组,定义了一个 `print_permutation` 函数来打印全排列,定义了一个 `swap` 函数来交换数组中的两个元素,定义了一个 `johnson_trotter` 函数来执行 Johnson Trotter 算法,最后在 `main` 函数中调用这些函数来生成全排列
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

铭毅天下

和你一起,死磕Elastic!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值