zoukankan      html  css  js  c++  java
  • Permute Digits

    C. Permute Digits
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    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
    Copy
    123
    222
    output
    Copy
    213
    input
    Copy
    3921
    10000
    output
    Copy
    9321
    input
    Copy
    4940
    5000
    output
    Copy
    4940


    一道cf的c题,挺好的,直接用字符串的操作可以很快的解决.
     1 #include <iostream>
     2 #include <algorithm>
     3 #define ll long long int
     4 using namespace std;
     5 string s,ss;
     6 string st;
     7 int main(){
     8     cin>>s>>ss;
     9     sort(s.begin(),s.end());
    10     if(ss.length()>s.length()){
    11         reverse(s.begin(),s.end());
    12         cout<<s<<endl;
    13         return 0;
    14     }
    15     for(int i=0;i<s.length();i++)
    16         for(int j=s.length()-1;j>i;j--){
    17             st=s;
    18             swap(s[i],s[j]);
    19             sort(s.begin()+i+1,s.end());
    20             if(s.compare(ss)>0)
    21                 s=st;
    22             else break;
    23         }
    24     cout<<s<<endl;
    25     return 0;
    26 }
  • 相关阅读:
    【尺取法】
    [USACO12MAR]花盆Flowerpot [单调队列]
    数据库笔记
    NYOJ 91 阶乘之和(贪心)
    NYOJ 71 独木舟上的旅行(贪心)
    水池数目(DFS)
    poj 1164城堡问题(DFS)
    NYOJ 12 喷水装置(二)( 贪心)
    NYOJ 6(贪心)
    NYOJ 45( 分治,大数)
  • 原文地址:https://www.cnblogs.com/zllwxm123/p/9314589.html
Copyright © 2011-2022 走看看