uva 146 - ID Codes

本文介绍了一个算法问题的解决方案:寻找给定序列的下一个字典序序列。通过逆向查找第一个ASCII码小于其后一位字符的位置,利用STL函数next_permutation确定下一序列。

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

 此题就是寻找给定序列的下一个序列(按照字典序)

//
//  main.cpp
//  uva 146 - ID Codes
//
//  Created by XD on 15/7/25.
//  Copyright (c) 2015年 XD. All rights reserved.
//

//解题思想:比当前序列大的序列与当前序列的特征是,前面部分相同,后面不同,不同部分的长度为 从当前序列逆向的第一个字母(位置为i)的ascii码//小于位置为i+1的字母的assii码
#include <iostream>
#include <string>
#include <queue>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include<vector>
#include <string.h>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <cstdio>
using namespace std ;

int main(int argc, const char * argv[]) {
    char s[55] ;
    while (scanf(" %s" , s)==1&&s[0]!='#') {
        int len = (int ) strlen(s) ;
        int start =-1 ;
        for (int i = len-1; i >0; i--) {
            if (s[i] <= s[i-1]) {
                continue ;
            }else{
                start = i-1 ;
                break ;
            }
        }
        if (start ==-1) {
            printf("No Successor\n") ;
        }
        else{
            //使用stl函数来确定下一个序列是什么,然后拼接起来就可以了
            char temp[55] ;
            for (int i = 0; i < len - start; i++) {
                temp[i] = s[i+start] ;
            }
            temp[len-start] = '\0' ;
            next_permutation(temp, temp + len - start) ;
            for (int i = 0 ; i < start; i++) {
                printf("%c" , s[i]) ;
            }
            for (int i = start; i < len; i++) {
                printf("%c"  ,  temp[i- start]) ;
            }
            printf("\n") ;
        }
    }
    
    
    
    
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值