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;
    }
    

      

  • 相关阅读:
    MP3 Lame 转换 参数 设置(转)
    数据库SQL优化大总结之 百万级数据库优化方案(转)
    IndexedDB:浏览器里内置的数据库(转)
    【译】微型ORM:PetaPoco【不完整的翻译】(转)
    'NoneType' object has no attribute '__getitem__'
    关于python的整形(int)自动转长整形(long)的问题
    1-redis使用笔记
    Flask deployment on gunicorn with flask script
    Celery从入门到放弃
    SELECT中的CAST
  • 原文地址:https://www.cnblogs.com/l609929321/p/9720806.html
Copyright © 2011-2022 走看看