PKU 1731.1256.1146 (next_permutation 全排序)

本文介绍了如何使用STL中的next_permutation和prev_permutation函数解决字符全排序问题,并通过两道具体题目进行实践演示,展示了如何编写相应代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 
今天看到一个帖子,才知道STL中next_permutation,prev_permutation可以实现字符的全排序问题,STL有不少好东西啊…,顺便把几道相应的题给切了。

 

PKU  1731

#include "iostream"
#include "algorithm"
#include "string"

using namespace std;

int n,flag;
char s[201];
int main()
{
       while(gets(s))
       {
             flag=0;
             n=strlen(s);
             sort(s,s+n);   //要先排序
             printf("%s/n",s);
             while(next_permutation(s,s+n)) 
                   printf("%s/n",s);
       }
             return 0;
}                  
 

 

PKU  1256

#include "iostream"
#include "algorithm"
#include "string"

using namespace std;

int n,flag,test;
char s[201];
//这个要自己定义一个cmp函数
int cmp(char a,char b)
{
        if(tolower(a)==tolower(b))
            return a  < b;
        else
            return tolower(a) < tolower(b);
}
    
int main()
{
       scanf("%d",&test);
       getchar();
       while(test--)
       {
             gets(s);
             n=strlen(s);
             sort(s,s+n,cmp);
             printf("%s/n",s);
             while(next_permutation(s,s+n,cmp))
                   printf("%s/n",s);
       }
             return 0;
}                     

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值