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. Resolve Missing artifact Issue in maven

    https://jingyan.baidu.com/article/d621e8da0a5b192864913f79.html

  2. python自动化运维笔记3 —— dns处理模块dnspython

    1.3 DNS处理模块 dnspython是python实现的一个DNS工具包,它支持几乎所有的记录类型,可以用于查询.传输并动态更新ZONE信息,同时支持TSIG(事物签名)验证消息和EDNS0(扩 ...

  3. delphi DBGRID 刷新定位问题 [问题点数:0分]

    我程序是 adoquery+datasource+dbgrid 做的我有一个窗体:有四个按钮.分别是新建,修改,删除,刷新. 新建第一条记录,dbgrid显示一条记录,新建第二条记录.DBGRID总共 ...

  4. Ajax 響應

    獲取服務器的響應內容,可以使用responseText或者responseXML屬性 responseText:獲取字符串形式的相應內容,除了XML的響應內容以外可用 responseXML:獲取XM ...

  5. 02 基于umi搭建React快速开发框架(国际化)

    前言 之前写过一篇关于React的国际化文章,主要是用react-intl库,雅虎开源的.react-intl是用高阶组件包装一层来做国际化. 基于组件化会有一些问题,比如在一些工具方法中需要国际化, ...

  6. codeforces622B

    The Time CodeForces - 622B 给你当前的时间(24小时制):HH:MM.输出 x 分钟后的时间是多少?(24小时制) 不明白可以看看例子哦- Input 第一行给出了当前时间, ...

  7. Nginx ACCESS阶段 如何限制IP访问

    192.168.1.0/24(最大32位的子网掩码) 每个ip是8位 那么 24/8 = 3 也就是前三个二进制 是 11111111 11111111 11111111 是指子网掩码的位数.写的是多 ...

  8. Chinese Postman Problem Aizu - DPL_2_B(无向图中国邮路问题)

    题意: 带权无向图上的中国邮路问题:一名邮递员需要经过每条边至少一次,最后回到出发点,一条边多次经过权值要累加,问最小总权值是多少.(2 <= N <= 15, 1 <= M < ...

  9. 【题解】 Codeforces Edu44 F.Isomorphic Strings (字符串Hash)

    题面戳我 Solution 我们按照每个字母出现的位置进行\(hash\),比如我们记录\(a\)的位置:我们就可以把位置表示为\(0101000111\)这种形式,然后进行字符串\(hash\) 每 ...

  10. 06 Zabbix分布式监控和主被动模式

    06 Zabbix分布式监控和主被动模式 zabbix proxy设置 使用zabbix代理的好处 监控拥有不可靠的远程区域 当监控项目数以万计的时候使用代理分担zabbix-proxy压力 简化分布 ...