全排列算法

来源:互联网 发布:如何得网游数据库信息 编辑:程序博客网 时间:2024/04/29 08:45

 全排列在很多程序都有应用,是一个很常见的算法,常规的算法是一种递归的算法,这种算法的得到基于以下的分析思路。  给定一个具有n个元素的集合(n>=1),要求输出这个集合中元素的所有可能的排列。

        一、递归实现

        例如,如果集合是{a,b,c},那么这个集合中元素的所有排列是{(a,b,c),(a,c,b),(b,a,c),(b,c,a),(c,a,b),(c,b,a)},显然,给定n个元素共有n!种不同的排列,如果给定集合是{a,b,c,d},可以用下面给出的简单算法产生其所有排列,即集合(a,b,c,d)的所有排列有下面的排列组成:

     (1)以a开头后面跟着(b,c,d)的排列

    (2)以b开头后面跟着(a,c,d)的排列

    (3)以c开头后面跟着(a,b,d)的排列

    (4)以d开头后面跟着(a,b,c)的排列,这显然是一种递归的思路,于是我们得到了以下的实现:

[cpp] view plaincopy
  1. #include "iostream"  
  2. using namespace std;  
  3.   
  4. void permutation(char* a,int k,int m)  
  5. {  
  6.     int i,j;  
  7.     if(k == m)  
  8.     {  
  9.         for(i=0;i<=m;i++)  
  10.             cout<<a[i];  
  11.         cout<<endl;  
  12.     }  
  13.     else  
  14.     {  
  15.         for(j=k;j<=m;j++)  
  16.         {  
  17.             swap(a[j],a[k]);  
  18.             permutation(a,k+1,m);  
  19.             swap(a[j],a[k]);  
  20.         }  
  21.     }  
  22. }  
  1. int main(void)  
  2. {  
  3.     char a[] = "abc";  
  4.     cout<<a<<"所有全排列的结果为:"<<endl;  
  5.     permutation(a,0,2);  
  6.     system("pause");  
  7.     return 0;  
  8. }  

    实现输出部分数的全排列,总共有len个字符,输出num个数的全排列

void permutation(char* a,int k,int len,int num)  
{  
    int i,j;  
    if(k == num)  
    {  
        for(i=0;i<num;i++)  
            cout<<a[i];  
        cout<<endl;  
    }  
    else  
    {  
        for(j=k;j<len;j++)  
        {  
            swap(a[j],a[k]);  
            permutation(a,k+1,len,num);  
            swap(a[j],a[k]);  
        }  
    }  
}

二、STL实现

        有时候递归的效率使得我们不得不考虑除此之外的其他实现,很多把递归算法转换到非递归形式的算法是比较难的,这个时候我们不要忘记了标准模板库已经实现的那些算法,这让我们非常轻松。STL有一个函数next_permutation(),它的作用是如果对于一个序列,存在按照字典排序后这个排列的下一个排列,那么就返回true且产生这个排列,否则返回false。注意,为了产生全排列,这个序列要是有序的,也就是说要调用一次sort。实现很简单,我们看一下代码:

[cpp] view plaincopy
  1. #include "iostream"  
  2. #include "algorithm"  
  3. using namespace std;  
  4.   
  5. void permutation(char* str,int length)  
  6. {  
  7.     sort(str,str+length);  
  8.     do  
  9.     {  
  10.         for(int i=0;i<length;i++)  
  11.             cout<<str[i];  
  12.         cout<<endl;  
  13.     }while(next_permutation(str,str+length));  
  14.   
  15. }  
  16. int main(void)  
  17. {  
  18.     char str[] = "acb";  
  19.     cout<<str<<"所有全排列的结果为:"<<endl;  
  20.     permutation(str,3);  
  21.     system("pause");  
  22.     return 0;  
  23. }  

          三、有一定约束条件的全排列

         对数1,2,3,4,5要实现全排序。要求4必须在3的左边,其它的数位置随意。 

            思路:首先使用上面的2种方法之一实现全排列,然后对全排列进行筛选,筛选出4在3左边的排列。

[cpp] view plaincopy
  1. #include "iostream"  
  2. #include "algorithm"  
  3. using namespace std;  
  4.   
  5. void permutation(int* a,int length)  
  6. {  
  7.     int i,flag;  
  8.     sort(a,a+length);  
  9.     do  
  10.     {  
  11.         for(i=0;i<length;i++)  
  12.         {  
  13.             if(a[i]==3)  
  14.                 flag=1;  
  15.             else if(a[i]==4)             //如果3在4的左边,执行完代码,flag就是2  
  16.                 flag=2;  
  17.         }  
  18.         if(flag==1)          //如果4在3的左边,执行完代码,flag就是1  
  19.         {  
  20.             for(i=0;i<length;i++)  
  21.                 cout<<a[i];  
  22.             cout<<endl;  
  23.         }  
  24.     }while(next_permutation(a,a+length));  
  25.   
  26. }  
  27. int main(void)  
  28. {  
  29.     int i,a[5];  
  30.     for(i=0;i<5;i++)  
  31.         a[i]=i+1;  
  32.     printf("%d以内所有4在3左边的全排列结果为:\n",i);  
  33.     permutation(a,5);  
  34.     system("pause");  
  35.     return 0;  
  36. }  
0 0
原创粉丝点击