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
123
222
output
213
input
3921
10000
output
9321
input
4940
5000
output4940 题目链接

题意
给两个数a和b,可以任意调换a中数字的位置,问由a变成的最大的不超过b的数是什么? 分析

暴搜加剪枝。因为结果一定存在,那么可以分为两种情况,lena<lenb或是lena==lenb。当lena<lenb时,从大到小输出a中的数即可。
当lena==lenb时,我们尽量保持和b的对应位相等,若一出现某位的数小于b上的数,剩下的数从大到小输出就好。
但是,会遇到前面都匹配的是相等的数,到了这一位,没有小于等于b对应位的数,这样构造出来的值就会大于b了,
这样子是不行的,所以我们得回溯,这样就需要dfs了。
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include <queue>
#include <vector>
#include<bitset>
using namespace std;
typedef long long LL;
const int maxn = ;
const int mod = +;
typedef pair<int,int> pii;
#define X first
#define Y second
#define pb push_back
//#define mp make_pair
#define ms(a,b) memset(a,b,sizeof(a)) const int inf = 0x3f3f3f3f;
char a[],b[],ans[];
int cnt[];
int lena,lenb;
bool dfs(int idx,int flag){
if(idx==lenb) return true; for(int ia=;ia>=;ia--){
if(cnt[ia]){
if(flag ||ia+''==b[idx]){//此前b已比ans大了
ans[idx]=ia+'';
cnt[ia]--;
if(dfs(idx+,flag)) return true;
cnt[ia]++;
}else if(ia+''<b[idx]){
ans[idx]=ia+'';
cnt[ia]--;
if(dfs(idx+,flag|))
return true;
}
}
}
return false;
}
int main(){
scanf("%s%s",a,b);
lena=strlen(a);
lenb=strlen(b); if(lena<lenb){
sort(a,a+lena);
for(int i=lena-;i>=;i--) putchar(a[i]);
}else{
ms(cnt,);
for(int i=;i<lena;i++) cnt[a[i]-'']++;
dfs(,);
ans[lena]=;
puts(ans);
}
}

 

CodeForces-915C Permute Digits的更多相关文章

  1. Codeforces 915 C. Permute Digits (dfs)

    题目链接:Permute Digits 题意: 给出了两个数字a,b(<=1e18),保证a,b都不带前缀0.用a的字符重组一个数字使这个值最大且小于b.(保证这个值存在) 题解: 这题遇到了不 ...

  2. cf Permute Digits(dfs)

    C. Permute Digits You are given two positive integer numbers a and b. Permute (change order) of the ...

  3. 【CodeForces 915 C】Permute Digits(思维+模拟)

    You are given two positive integer numbers a and b. Permute (change order) of the digits of a to con ...

  4. Permute Digits 915C

    You are given two positive integer numbers a and b. Permute (change order) of the digits of a to con ...

  5. 【Educational Codeforces Round 36 C】 Permute Digits

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] //从大到小枚举第i(1..len1)位 //剩余的数字从小到大排序. //看看组成的数字是不是小于等于b //如果是的话. //说 ...

  6. Codeforces Gym 100531D Digits 暴力

    Problem D. Digits 题目连接: http://codeforces.com/gym/100531/attachments Description Little Petya likes ...

  7. CF915C Permute Digits 字符串 贪心

    You are given two positive integer numbers a and b. Permute (change order) of the digits of a to con ...

  8. Permute Digits

    You are given two positive integer numbers a and b. Permute (change order) of the digits of a to con ...

  9. C. Permute Digits dfs大模拟

    http://codeforces.com/contest/915/problem/C 这题麻烦在前导0可以直接删除,比如 1001 100 应该输出11就好 我的做法是用dfs,每一位每一位的比较. ...

随机推荐

  1. 在WIN10打造成能运行Oracle的JDK的Linux

    1.开发WindowsFeature(程序)里的Linux扩展接口(其实从Windows2008发布前试用时,就发现悄悄藏着一个Unix接口选项). 2.在windows应用商店可以下载到Ubuntu ...

  2. Mybatis复杂嵌套关联一例

    Mybatis  three entity relation:association in collection PatentMapper.xml <resultMap id="Bas ...

  3. [转帖]关于hostnamectl 命令

    作者:Linux运维 来源:CSDN 原文:https://blog.csdn.net/linuxnews/article/details/51112022 版权声明:本文为博主原创文章,转载请附上博 ...

  4. python之字典操作

    字典操作代码如下: #数据字典操作汇总 person = {'name': 'Mike', 'age': 25} print("初始的数据字典:", person) #访问字典值 ...

  5. 聊聊我怎么系统学习Linux技能并快速提高的

    随着电子信息科技时代的发展,学会使用计算机在我们的生活中成为了必不可少的一项技能.而作为计算机中的三大操作系统之一的Linux更是饱受计算机爱好者们的喜爱.今天我们就来和大家一起聊一聊Linux操作系 ...

  6. Django-website 程序案例系列-1 最简单的web服务器

    templates:html模板文件夹下面建立文件login.html <!DOCTYPE html> <html lang="en"> <head& ...

  7. Bicriterial routing 双调路径 HYSBZ - 1375(分层最短路)

    Description 来越多,因此选择最佳路径是很现实的问题.城市的道路是双向的,每条道路有固定的旅行时间以及需要支付的费用.路径由连续的道路组成.总时间是各条道路旅行时间的和,总费用是各条道路所支 ...

  8. Java 入门进阶

    Java 入门进阶 發表於 2015-04-16 http://xielong.me/2015/04/16/%E6%9C%8D%E5%8A%A1%E7%AB%AF%E5%B7%A5%E7%A8%8B% ...

  9. BZOJ 3526: [Poi2014]Card

    3526: [Poi2014]Card Time Limit: 25 Sec  Memory Limit: 64 MBSubmit: 267  Solved: 191[Submit][Status][ ...

  10. List does not exist. The page you selected contains a list that does not exist. It may have been deleted by another user

    当我在subsite里点击"Add a document",报这个错,后来一看event log: 在AAM里加上一条: 问题搞定: