zoukankan      html  css  js  c++  java
  • 【个人训练】(UVa146)ID Codes

    题意与解析

    这题其实特别简单,求给定排列的后继。使用stl(next_permutation)可以方便地解决这个问题。但是,想要自己动手解就是另外一回事了。我的解法是从后往前找到第一个$a_i$比$a_j$小($i<j$)的,然后交换之,接下来i->end范围内重排序即可。

    代码

    /* ***********************************************
    Author        :Sam X
    Created Time  :2018年01月16日 星期二 08时44分26秒
    File Name     :uva146.cpp
    ************************************************ */
    
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <string>
    #include <cmath>
    #include <cstdlib>
    #include <vector>
    #include <queue>
    #include <set>
    #include <map>
    #include <algorithm>
    #include <iterator>
    using namespace std;
    
    typedef long long ll;
    typedef unsigned long long ull;
    
    int main()
    {
        //freopen("input.txt","r",stdin);
        //freopen("output.txt","w",stdout);
        string str;
        while(cin>>str)
        {
            bool ok=false;
            if(str[0]=='#') break;
            int len=str.length();
            for(int i=len-1;i>=1;--i)
            {
                for(int j=i-1;j>=0;--j)
                {
                    if(str[j]<str[i])
                    {
                        ok=true;
                        swap(str[i],str[j]);
                        auto iter=str.begin();
                        advance(iter,j+1);
                        sort(iter,str.end());
                        break;
                    }
                }
                if(ok) break;
            }
            if(ok) cout<<str<<endl;
            else cout<<"No Successor"<<endl;
        }
    
        return 0;
    }
    如非注明,原创内容遵循GFDLv1.3发布;其中的代码遵循GPLv3发布。
  • 相关阅读:
    FactoryBean的作用
    ztree点击文字勾选checkbox,radio实现方法
    js判断字符长度 汉字算两个字符
    双系统引导修复
    thinkpadT440p
    分布式服务器集群
    Eclipse插件安装方式
    用SourceTree轻巧Git项目图解
    廖雪峰git使用完整教程
    hessian
  • 原文地址:https://www.cnblogs.com/samhx/p/UVa-146.html
Copyright © 2011-2022 走看看