zoukankan      html  css  js  c++  java
  • CF915C Permute Digits 字符串 贪心

    You are given two positive integer numbers a and b. Permute (change order) of the digits of a to construct maximal number not exceeding b. No number in input and/or output can start with the digit 0.

    It is allowed to leave a as it is.

    Input

    The first line contains integer a (1 ≤ a ≤ 1018). The second line contains integer b(1 ≤ b ≤ 1018). Numbers don't have leading zeroes. It is guaranteed that answer exists.

    Output

    Print the maximum possible number that is a permutation of digits of a and is not greater than b. The answer can't have any leading zeroes. It is guaranteed that the answer exists.

    The number in the output should have exactly the same length as number a. It should be a permutation of digits of a.

    Examples

    Input
    123
    222
    Output
    213
    Input
    3921
    10000
    Output
    9321
    Input
    4940
    5000
    Output
    4940

    题意:给你两个小于10^18的数a,b,a的每一位的数可以随意排列,求所得到的小于b的最大a
    分析:每次暴力判断每一位数放前面时后面取最大是否大于b,如果不大于则当前位取这个数是最佳
    AC代码:
    #include <map>
    #include <set>
    #include <stack>
    #include <cmath>
    #include <queue>
    #include <cstdio>
    #include <vector>
    #include <string>
    #include <bitset>
    #include <cstring>
    #include <iomanip>
    #include <iostream>
    #include <algorithm>
    #include <bits/stdc++.h>
    #define ls (r<<1)
    #define rs (r<<1|1)
    #define debug(a) cout << #a << " " << a << endl
    using namespace std;
    typedef long long ll;
    const ll maxn = 1e4+10;
    const ll mod = 1000000007;
    const double pi = acos(-1.0);
    const double eps = 1e-8;
    bool cmp( char p, char q ) {
        return p > q;
    }
    int main() {
        ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
        string s1, s2;
        while( cin >> s1 >> s2 ) {
            ll len1 = s1.length(), len2 = s2.length();
            sort(s1.begin(),s1.end());
            if( len1 < len2 ) {
                reverse(s1.begin(),s1.end());
                cout << s1 << endl;
                continue;
            }
            for( ll i = 0; i < len1; i ++ ) {
                for( ll j = len1-1; j > i; j -- ) {
                    string t = s1;
                    swap(s1[i],s1[j]);
                    sort(s1.begin()+i+1,s1.end());
                    if( s1 > s2 ) {
                        s1 = t;   //每次循环确定一个当前所能取到的最小值中的最大值数字
                    } else {
                        break;
                    }
                }
                //debug(s1);
            }
            cout << s1 << endl;
        }
        return 0;
    }
    

      

  • 相关阅读:
    input宽度自适应
    七牛云上传 图片
    删除文件夹所有内容
    无法获取 div123 的内部内容,因为该内容不是文本。
    关于"无法删除数据库 'xxx',因为该数据库当前正在使用" 解决办法
    Asp.Net 获取FileUpload控件的文件路径、文件名、扩展名
    asp.net复制整个文件夹
    多行文本框内容转换空格换行
    花(唯一分解定理+排列组合+快速幂)
    Xn数列(矩阵乘法+快速幂+慢速乘法)
  • 原文地址:https://www.cnblogs.com/l609929321/p/9720806.html
Copyright © 2011-2022 走看看