zoukankan      html  css  js  c++  java
  • UVALIVE 4556 The Next Permutation

    4556 The Next Permutation
    For this problem, you will write a program that takes a (possibly long) string of decimal digits, and
    outputs the permutation of those decimal digits that has the next larger value (as a decimal number)
    than the input number. For example:
    123 -> 132
    279134399742 -> 279134423799
    It is possible that no permutation of the input digits has a larger value. For example, 987.
    Input
    The first line of input contains a single integer P , (1 ≤ P ≤ 1000), which is the number of data sets that
    follow. Each data set is a single line that contains the data set number, followed by a space, followed
    by up to 80 decimal digits which is the input value.
    Output
    For each data set there is one line of output. If there is no larger permutation of the input digits,
    the output should be the data set number followed by a single space, followed by the string ‘BIGGEST’.
    If there is a solution, the output should be the data set number, a single space and the next larger
    permutation of the input digits.
    Sample Input
    3
    1 123
    2 279134399742
    3 987
    Sample Output
    1 132
    2 279134423799
    3 BIGGEST

    題意:操作字符串,通過調整字符串的順序得到一個新的字符串,新的字符串的数值是比原字符串的数大的最小值;

    题解:由于是最小值,我们可以从后往前遍历字符串,当str[i]>str[i-1]时,pos=i-1,此位置就是我们要调整字符串的起始位置(说明我们可以调整顺序使原来的字符串的值增大),然后从我们要截取的字串中找到第一个比上pos大的值交换,然后将字串排序即可,若没有条件满足str[i]>str[i-1],则通过调整字符顺序没有比原串更大的数值。

    #include<stdio.h>
    #include<string.h>
    #include<stack>
    #include<queue>
    #include<iostream>
    #include<algorithm>
    #include<map>
    #include<vector>
    #define PI acos(-1.0)
    using namespace std;
    typedef long long ll;
    int m,n,k,p;
    int ans[1001];
    int dis[500][500];
    int di[4][2]= {{-1,0},{1,0},{0,-1},{0,1}};
    map<ll,ll>::iterator it;
    int main()
    {
        cin>>m;
        while(m--)
        {
            string str;
            cin>>n>>str;
            int pos=-1;
            for(int i=str.size()-1; i>=1; i--)
            {
                if(str[i]>str[i-1])
                {
                    pos=i-1;
                    break;
                }
            }
            if(pos==-1)
            {
                cout<<n<<" "<<"BIGGEST"<<endl;
                continue;
            }
            string s,arr;
            s=str.substr(pos);
            int cnt=s[0];
            for(int i='0'; i<='9'; i++)
            {
    
                if(s.find(i)!=string::npos&&i>cnt)
                {
                    swap(s[0],s[s.find(i)]);
                    break;
                }
            }
            sort(s.begin()+1,s.end());
            cout<<n<<" "<<str.substr(0,pos)<<s<<endl;;
        }
    
    }
    View Code
  • 相关阅读:
    百度贴吧营销实战之推广入口布局技巧
    微信加人限制2017版
    百度经验怎么赚钱之练就三星经验,轻松布局流量入口。
    百科词条 如何创建?客源何在?怎么赚钱?
    人物百科 从词条创建到最终盈利(简剖)
    百度文库营销技巧之文库不为人知的通过率 以及排名推广技巧
    百度知道一周练就5级账号(养号系列一)
    百度知道账号,快速升级必备36计(养号系列二)
    QQ运动,新楛的马桶还在香,营销人不应摒弃。
    裂变营销 流量分散年代 你那样的不可或缺
  • 原文地址:https://www.cnblogs.com/moomcake/p/9787756.html
Copyright © 2011-2022 走看看