ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

全排列函数next_permutation函数

2022-02-08 19:00:39  阅读:150  来源: 互联网

标签:排列 函数 int next ch num permutation


转载于: C++中全排列函数next_permutation 用法_Marcus-Bao的个人主页-CSDN博客_c++ next_permutation

全排列参考了两位的博客 感谢!

http://blog.sina.com.cn/s/blog_9f7ea4390101101u.html

http://blog.csdn.net/ac_gibson/article/details/45308645

早就听说了了next_permutation 产生全排列的强大,一直到昨晚遇到一个对字符串产生全排列的问题才知道这个函数的强大,我们队是按照dfs去搞全排列,然后在进行字符串的匹配,结果写的很长,过程中还各种debug。。。于是决定今天学一下...

next_permutation函数详解

    组合数学中经常用到排列,这里介绍一个计算序列全排列的函数:next_permutation(start,end),和prev_permutation(start,end)。这两个函数作用是一样的,区别就在于前者求的是当前排列的下一个排列,后一个求的是当前排列的上一个排列。至于这里的“前一个”和“后一个”,我们可以把它理解为序列的字典序的前后,严格来讲,就是对于当前序列pn,他的下一个序列pn+1满足:不存在另外的序列pm,使pn<pm<pn+1.

对于next_permutation函数,其函数原型为:

 #include <algorithm>

 bool next_permutation(iterator start,iterator end)

当当前序列不存在下一个排列时,函数返回false,否则返回true

我们来看下面这个例子:

#include <iostream>  
#include <algorithm>  
using namespace std;  
int main()  
{  
    int num[3]={1,2,3};  
    do  
    {  
        cout<<num[0]<<" "<<num[1]<<" "<<num[2]<<endl;  
    }while(next_permutation(num,num+3));  
    return 0;  
}  

输出结果:

1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1

当我们把while(next_permutation(num,num+3))中的3改为2时,输出就变为了:

1 2 3
2 1 3

由此可以看出,next_permutation(num,num+n)函数是对数组num中的前n个元素进行全排列,同时并改变num数组的值。

另外,需要强调的是,next_permutation()在使用前需要对欲排列数组按升序排序,否则只能找出该序列之后的全排列数。比如,如果数组num初始化为2,3,1,那么输出就变为了:

2 3 1
3 1 2
3 2 1

此外,next_permutation(node,node+n,cmp)可以对结构体num按照自定义的排序方式cmp进行排序。

也可以对字符...

next_permutation 自定义比较函数 POJ 1256

题目中要求的字典序是

//'A'<'a'<'B'<'b'<...<'Z'<'z'.

。。。
 

#include<iostream> //poj 1256 Anagram
#include<string>
#include<algorithm>
using namespace std;
int cmp(char a, char b)
{
    if (tolower(a) != tolower(b))//tolower 是将大写字母转化为小写字母.
        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;

}

例题

题目描述:

就是把几个字符串首尾拼接,可重合的部分去掉。求最小长度。

思路:

这个题用next_permutation 产生全排列 然后用substr产生子串去判断是否可链接 比写dfs省事多了 我也是通过这个题终于觉得学一下了

#include<bits/stdc++.h>
#define PT printf
#define SC scanf
using namespace std;
const int maxn=1e6+10;
typedef long long ll;
string s[20];
    int t,n;
int solve()
{
    string str=s[1];
    for(int i=2;i<=n;i++)
    {
        int len=min(str.size(),s[i].size());
        for(int j=len;j>0;j--)
        {
            if(str.substr(str.size()-j,j)==s[i].substr(0,j))
            {
                str+=s[i].substr(j,s[i].size()-j);
                break;
            }
            if(j==1)
            {
                str+=s[i];
            }
        }
    }
    return str.size();
}
int main()
{
 
    int k=1;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        cin>>s[i];
        sort(s+1,s+1+n);
        int ans=999999;
        do{
            ans=min(ans,solve());
        }while(next_permutation(s+1,s+1+n));
        printf("Case %d: %d\n",k++,ans);
    }
     return 0;
}

 

标签:排列,函数,int,next,ch,num,permutation
来源: https://www.cnblogs.com/luyufan/p/15871896.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有