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. HDU 2011 多项式求和

    http://acm.hdu.edu.cn/showproblem.php?pid=2011 Problem Description 多项式的描述如下:1 - 1/2 + 1/3 - 1/4 + 1/ ...

  2. Docker Dockerfile指令

    Docker 可以通过 Dockerfile 的内容来自动构建镜像.Dockerfile 是一个包含创建镜像所有命令的文本文件,通过docker build命令可以根据 Dockerfile 的内容构 ...

  3. [转帖]什么是Asp.net Core?和 .net core有什么区别?

    什么是Asp.net Core?和 .net core有什么区别? https://www.cnblogs.com/itzhangxp/p/8322364.html 知道微软开始用 kestrel了 ...

  4. zabbix 使用问题两个--中文乱码,以及监控ESXi下的虚拟机

    1. 中文乱码 最开始中文显现 长方形不显示文字.解决办法: c:\windows\fonts 下面复制 楷体的字体(字体随意看自己喜欢) 文件名一般为: simkai.ttf 2.将simkai.t ...

  5. Angular require(抄别的)

    require参数的值可以用下面的前缀进行修饰,这会改变查找控制器时的行为:?如果在当前指令中没有找到所需要的控制器,会将null作为传给link函数的第四个参数.^如果添加了^前缀,指令会在上游的指 ...

  6. scipy积分 integral

    #!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2018/5/24 15:03 # @Author : zhang chao # @Fi ...

  7. SVN上线步骤笔记

    项目代码位置: /data/svn/play_out 项目代码目录名称: test SVN创建位置:/data/svn/repos_Websvn线上地址:svn://192.168.1.1/repos ...

  8. mysql学习笔记五 —— MHA

    MySQL_MHA ABB(主从复制)-->MHA(实现mysql高可用.读写分离.脚本控制vip飘逸)-->haproxy(对slave集群实现分发,负载均衡)-->keepali ...

  9. BZOJ4182 Shopping(点分治+树形dp)

    点分治,每次考虑包含根的连通块,做树形多重背包即可,dfs序优化.注意题面给的di范围是假的,坑了我0.5h,心态炸了. #include<iostream> #include<cs ...

  10. LOJ #2434. 「ZJOI2018」历史(LCT)

    题意 click here 题解 我们首先考虑答案是个什么样的东西, 不难 发现每个点可以单独计算它的贡献. 令每个点 \(i\) 崛起次数为 \(a_i\) . 假设一个点子树的 \(\sum a_ ...