uva 全排列题目 146 + 729 + 10098

本文介绍了如何利用C++ STL中的next_permutation函数实现字符串的全排列,通过三个UVa在线评测系统的题目示例,展示了该函数的具体应用及其实现全排列的便捷性。

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


解题思路: 对于全排列的问题,STL,提供了一个强大的函数,

bool next_permutation(iterator.begin() , iterator.end());

对于当前的序列如果不是最后一个序列则返回真,否则返回假。


uva 10098 - Generating Fast

题目链接:点击打开链接

代码:
#include <iostream>
#include <cstdio>
#include <string>
#include <cstdlib>
#include <algorithm>
using namespace std;

string str;
int main(){
    int n;
    scanf("%d" , &n);
    while(n--){
        string str;
        cin>>str;
        while(next_permutation(str.begin() , str.end())){
            cout<<str<<endl;
        }
        cout<<endl;
    }
    return 0 ;
}

uva 729 - The Hamming Distance Problem

题目链接:点击打开链接

代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
using namespace std;

int main() {
    int n, h;
    string str;
    scanf("%d%d", &n, &h);
    for (int i = 0; i < n; i++) {
        if (i >= n - h)
            str += '1';
        else
            str += '0';
    }
    cout<<str<<endl;
    while (next_permutation(str.begin(), str.end()))
        cout << str << endl;
    return 0;
}


uva 146 - ID Codes

题目链接:点击打开链接

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <algorithm>
using namespace std;

string str;

int main(){
    while(cin>>str){
        if(str == "#")
            break;
        if(next_permutation(str.begin(),str.end()))
            cout<<str<<endl;
        else
            printf("No Successor\n");
    }  
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值