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. javaIO缓冲区

    java中IO类分类. 图来自网络 缓冲区:应用程序在内存中开辟的一个空间.用来放置需要被写入或写出的数据. 使用缓冲区的 优点:使得应用程序操作磁盘(或者说是与磁盘的通信)的次数降低,提高应用程序的 ...

  2. arctan

    ArcTanWhen the ArcTan functional configuration is selected, the input vector (X_IN,Y_IN) is rotated( ...

  3. linux 内核参数优化----最大线程数限制及当前线程数查询

    1.总结系统限制有: /proc/sys/kernel/pid_max #查系统支持的最大线程数,一般会很大,相当于理论值 /proc/sys/kernel/thread-max max_user_p ...

  4. Luogu3804 【模板】后缀自动机(后缀自动机)

    建出parent树统计即可.开始memcpy处写的是sizeof(son[y]),然后就T掉了……还是少用这种东西吧. 同时也有SA做法.答案子串一定是名次数组中相邻两个串的lcp.单调栈统计其是几个 ...

  5. Food HDU - 4292 (结点容量 拆点) Dinic

    You, a part-time dining service worker in your college’s dining hall, are now confused with a new pr ...

  6. mysql test== 坑

    错误 <if test="status == '1'"> 正确 <if test="status == '1'.toString()">

  7. hihoCoder#1743:K-偏差排列(矩阵快速幂+状压dp)

    题意 如果一个 \(1\to N\) 的排列 \(P=[P_1, P_2, ... P_N]\) 中的任意元素 \(P_i\) 都满足 \(|P_i-i| ≤ K\) ,我们就称 \(P\) 是 \( ...

  8. Leetcode 191.位1的个数 By Python

    编写一个函数,输入是一个无符号整数,返回其二进制表达式中数字位数为 '1' 的个数(也被称为汉明重量). 示例 : 输入: 11 输出: 3 解释: 整数 11 的二进制表示为 00000000000 ...

  9. 【BZOJ2227】[ZJOI2011]看电影(组合数学,高精度)

    [BZOJ2227][ZJOI2011]看电影(组合数学,高精度) 题面 BZOJ 洛谷 题解 这题太神仙了. 首先\(K<N\)则必定无解,直接特判解决. 现在只考虑\(K\ge N\)的情况 ...

  10. BZOJ 400题纪念

    应该是最后一次纪念了吧! 想当年,我可是发过"BZOJ 10题纪念"的人--那时候(一年前?)的自己真的好菜啊,只能说掌握了c++的基础语法的样子.当时觉得省选级别的BZOJ题是世 ...