码迷,mamicode.com
首页 > 其他好文 > 详细

Permute Digits

时间:2018-07-15 21:29:28      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:div   nta   style   hat   main   exce   def   cin   pie   

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 }

 

Permute Digits

标签:div   nta   style   hat   main   exce   def   cin   pie   

原文地址:https://www.cnblogs.com/zllwxm123/p/9314589.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!