UVA - 146 ID Codes

题目大意:给出一串字符,将这些字母升序全排列后,求该串字符的下一串,若是最后一串,输出 No Successor

解题思路:从后往前扫,碰到第一个 s[i] > s[i-1] 的时候,用 i(包括)后的大于 s[i-1] 当中最小的和 s[i-1] 换位置,然后对 i(包括)后的字母排升序。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
bool cmp(char a, char b) {
    return a < b;
}
int main() {
    char s[100];
    while (scanf("%s", s) != EOF && s[0] != '#') {
        int len = strlen(s);
        int tag = 0;
        for (int i = len-1; i > 0; i--)
            if (s[i] > s[i-1]) {
                tag = 1;
                sort(&s[i], s+len, cmp);
                int t = i;
                while (s[i-1] >= s[t]) t++;
                char c = s[i-1];
                s[i-1] = s[t];
                s[t] = c;
                sort(&s[i], s+len, cmp);
                break;
            }
        if (tag) printf("%s\n", s);
        else printf("No Successor\n");

    }
return 0;
}

STL:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
char s[100];
int main() {
    while (cin >> s && s[0] != '#') {
        if (next_permutation(s, s+strlen(s)))
                cout<<s<<endl;
        else cout<<"No Successor"<<endl;
    }
return 0;
}

STL 简直黑科技…

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值