zoukankan      html  css  js  c++  java
  • stl 之 next_permutation 求出一个排序的下一个排列的函数 转载

    这是一个求一个排序的下一个排列的函数,可以遍历全排列,要包含头文件<algorithm>
    下面是以前的笔记    与之完全相反的函数还有prev_permutation
     
     
    (1) int 类型的next_permutation
     
    int main()
    {
     int a[3];
    a[0]=1;a[1]=2;a[2]=3;
     do
    {
    cout<<a[0]<<" "<<a[1]<<" "<<a[2]<<endl;
    } while (next_permutation(a,a+3)); //参数3指的是要进行排列的长度
     
    //如果存在a之后的排列,就返回true。如果a是最后一个排列没有后继,返回false,每执行一次,a就变成它的后继
     
     
    }
     
    输出:
     
     1 2 3
     1 3 2
     2 1 3
     2 3 1
     3 1 2
     3 2 1
     
     
    如果改成 while(next_permutation(a,a+2));
    则输出:
     1 2 3
     2 1 3
     
    只对前两个元素进行字典排序
    显然,如果改成 while(next_permutation(a,a+1)); 则只输出:1 2 3
     
     
     
    若排列本来就是最大的了没有后继,则next_permutation执行后,会对排列进行字典升序排序,相当于循环
     
     int list[3]={3,2,1};
    next_permutation(list,list+3);
    cout<<list[0]<<" "<<list[1]<<" "<<list[2]<<endl;
     
    //输出: 1 2 3

     
     
     
     
    (2) char 类型的next_permutation
     
    int main()
    {
     char ch[205];
    cin >> ch;
     
    sort(ch, ch + strlen(ch) );
    //该语句对输入的数组进行字典升序排序。如输入9874563102 cout<<ch; 将输出0123456789,这样就能输出全排列了
     
     char *first = ch;
     char *last = ch + strlen(ch);
     
     do {
    cout<< ch << endl;
    }while(next_permutation(first, last));
     return 0;
    }
     
    //这样就不必事先知道ch的大小了,是把整个ch字符串全都进行排序
    //若采用 while(next_permutation(ch,ch+5)); 如果只输入1562,就会产生错误,因为ch中第五个元素指向未知
    //若要整个字符串进行排序,参数5指的是数组的长度,不含结束符

     
     
     
     
     
    (3) string 类型的next_permutation
     
    int main()
    {
     string line;
     while(cin>>line&&line!="#")
    {
     if(next_permutation(line.begin(),line.end())) //从当前输入位置开始
    cout<<line<<endl;
     else cout<<"Nosuccesor\n";
    }
    }
     
     
     
    int main()
    {
     string line;
     while(cin>>line&&line!="#")
    {
    sort(line.begin(),line.end());//全排列
    cout<<line<<endl;
     while(next_permutation(line.begin(),line.end()))
    cout<<line<<endl;
    }
    }
     
     
     
     
     
     
     next_permutation 自定义比较函数
     
     
    #include<iostream> //poj 1256 Anagram
    #include<string>
    #include<algorithm>
    using namespace std;
    int cmp(char a,char b) //'A'<'a'<'B'<'b'<...<'Z'<'z'.
    {
     if(tolower(a)!=tolower(b))
     return tolower(a)<tolower(b);
     else
     return a<b;
    }
    int main()
    {
     char ch[20];
     int n;
    cin>>n;
     while(n--)
    {
    scanf("%s",ch);
    sort(ch,ch+strlen(ch),cmp);
     do
    {
    printf("%s\n",ch);
    }while(next_permutation(ch,ch+strlen(ch),cmp));
    }
     return 0;
    }

    擅长排列的小明

    时间限制:1000 ms  |  内存限制:65535 KB
    难度:4
     
    描述
    小明十分聪明,而且十分擅长排列计算。比如给小明一个数字5,他能立刻给出1-5按字典序的全排列,如果你想为难他,在这5个数字中选出几个数字让他继续全排列,那么你就错了,他同样的很擅长。现在需要你写一个程序来验证擅长排列的小明到底对不对。
     
    输入
    第一行输入整数N(1<N<10)表示多少组测试数据,
    每组测试数据第一行两个整数 n m (1<n<9,0<m<=n)
    输出
    在1-n中选取m个字符进行全排列,按字典序全部输出,每种排列占一行,每组数据间不需分界。如样例
    样例输入
    2
    3 1
    4 2
    样例输出
    1
    2
    3
    12
    13
    14
    21
    23
    24
    31
    32
    34
    41
    42
    43

    /*题解:
    用STL中的next_permutation()解得,和D的小L(直接输出前n个数的排列)近似。
    区别在于仅输出前n个中的m个且各不相同。 
    */

    [cpp] view plaincopy在CODE上查看代码片派生到我的代码片
     
    1. #include<iostream>  
    2. #include<cstring>  
    3. #include<algorithm>   
    4. using namespace std;  
    5. int main()  
    6. {  
    7.     int T,n,m,i;  
    8.     char a[10]={'1','2','3','4','5','6','7','8','9','\0'},b[10],c[10];  
    9.     cin>>T;  
    10.     while(T--)  
    11.     {  
    12.         cin>>n>>m;  
    13.         for(i=0; i<n; i++)  
    14.             b[i] = a[i];  
    15.         b[m]='\0';  
    16.         cout<<b<<endl;  
    17.         while(next_permutation(a,a+n))  
    18.         {  
    19.             strcpy(c,a);  
    20.             c[m] = '\0';    
    21.             if(strcmp(b,c))//若与上一排列不相同,输出。   
    22.             {  
    23.                 cout<<c<<endl;  
    24.                 strcpy(b,c);  
    25.             }     
    26.         }  
    27.     }  
    28.     return 0;  
    29. }   
  • 相关阅读:
    操作系统__kali(1)基本操作指令,以及常用工具
    Log4net入门(回滚日志文件篇)
    Log4net入门(日志文件篇)
    Log4net入门(控制台篇)
    openwrt控制GPIO
    openwrt DTS介绍
    openwrt bin文件解析
    STM32(三十)蓝牙通信
    openwrt的led configuration
    uci文件生成脚本函数说明
  • 原文地址:https://www.cnblogs.com/luyi14/p/4224669.html
Copyright © 2011-2022 走看看