Permute Digits 915C
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
123
222
213
3921
10000
9321
4940
5000
4940
题解:
dfs,深搜,需要注意的是如果我们找到一个小于当前B中的值时,后面的只需要从小到大排列就好了
#include <bits/stdc++.h>
using namespace std;
char a[20],b[20];
int num[11],x[200],w[200],lb,la;
long long MAX=0,B;
inline long long MAx(long long z,long long w)
{
if(z>w) return z;
return w;
}
bool cmp(char a,char b)
{
return a>b;
} void dfs(long long ans,int len,int k,int v)
{
if(len==la&&len<=lb&&ans<=B) {
MAX=MAx(ans,MAX);
ans=0;
return;
}
for (int i = 9; i >=0 ; i--) {
if (num[i])
{
if(v==0&&i==w[k])
{
num[i]--;
ans=ans*10+i;
dfs(ans,len+1,k+1,0);
ans-=i;
ans/=10;
num[i]++;
} else if(i<w[k])
{
num[i]--;
ans=ans*10+i;
for (int j = 9; j >=0 ; j--) {
for (int z = 0; z <num[j] ; ++z) {
ans=ans*10+j;
}
}
if(ans<B)
{
MAX=MAx(MAX,ans);
}
ans=0;
}
}
}
}
int main()
{
scanf("%s",a);
scanf("%s",b);
la=(int)strlen(a);
lb=(int)strlen(b);
for (int i = 0; i <la ; ++i) {
num[a[i]-'0']++;
x[i]=a[i]-'0';
}
for (int i = 0; i<lb; i++) {
w[i]=b[i]-'0';
B=B*10+w[i];
}
if(la<lb)
{
sort(a,a+la,cmp);
for (int i = 0; i <la ; ++i) {
printf("%c",a[i]);
}
printf("\n");
return 0;
}
dfs(0,0,0,0);
printf("%lld",MAX);
return 0;
}
Permute Digits 915C的更多相关文章
- CodeForces-915C Permute Digits
C. Permute Digits time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- Codeforces 915 C. Permute Digits (dfs)
题目链接:Permute Digits 题意: 给出了两个数字a,b(<=1e18),保证a,b都不带前缀0.用a的字符重组一个数字使这个值最大且小于b.(保证这个值存在) 题解: 这题遇到了不 ...
- cf Permute Digits(dfs)
C. Permute Digits You are given two positive integer numbers a and b. Permute (change order) of the ...
- 【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 ...
- CF915C Permute Digits 字符串 贪心
You are given two positive integer numbers a and b. Permute (change order) of the digits of a to con ...
- Permute Digits
You are given two positive integer numbers a and b. Permute (change order) of the digits of a to con ...
- C. Permute Digits dfs大模拟
http://codeforces.com/contest/915/problem/C 这题麻烦在前导0可以直接删除,比如 1001 100 应该输出11就好 我的做法是用dfs,每一位每一位的比较. ...
- CF915C Permute Digits
思路: 从左到右贪心放置数字,要注意判断这个数字能否放置在当前位. 实现: #include <bits/stdc++.h> using namespace std; typedef lo ...
- 【Educational Codeforces Round 36 C】 Permute Digits
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] //从大到小枚举第i(1..len1)位 //剩余的数字从小到大排序. //看看组成的数字是不是小于等于b //如果是的话. //说 ...
随机推荐
- Struts2_总结
还未学习的内容,如果到时候要用到,再去学.1.Lamda 表达式(很复杂,很少用)2.验证框架(默认验证方法 validation.方法开始前验证.开始后验证)3.UI标签(用的不多)4.类型转换中的 ...
- Struts1.x 用户登录模块的实现
页面验证部分: <%@ page language="java" contentType="text/html; charset=UTF-8" pageE ...
- selenium server在页面加载超时浏览器与driver通信失败时的妙用
事实上,WebDriver有两种方式“驱动”浏览器的方式.1. Selenium Server:和Selenium RC一样的,通过指定远端服务器IP地址和端口号,由这个远端服务器来驱动浏览器.2. ...
- 分析一点python源代码
偶然看了一下python的部分源代码,感觉python的作者写的代码真心很美,简洁美观,学习之. 举几个例子抛砖引玉一下: def removedirs(name): ""&quo ...
- [topcoder]TheConsecutiveIntegersDivOne
http://community.topcoder.com/stat?c=problem_statement&pm=13625&rd=16278 首先,如果记得曼哈顿距离最小值那个问题 ...
- bootstrap-table 数据表格行内修改
bootstrap-table 数据行内修改js中设置列的属性 editable : { type : 'text',//数据显示在文本框内 emptytext : "--",// ...
- CentOS6下DHCP服务(一)工作原理及安装配置说明
1.DHCP服务用途 DHCP是Dynamic Host Configuration Protocol的简写,DHCP服务器最主要的工作就是自动地将网络参数分配给网络中的每台计算机,让客户端的计算机在 ...
- 初识Python(四)
一.数字数据类型 Python的数字数据类型用于存储数值,它是不可变的数据类型,这意味着改变数字数据类型,则需要一个新分配的对象: Python支持四种不同的数值类型: 整型(Int):通常被称为是整 ...
- Altium_Designer-PCB中各层作用详解
一直以来,对PCB中各层,比如:solder层.paste层.Top overlay层等等这些一知半解.今天仔细看了下,向大家介绍一下,有不对的地方还请指正. 1.mechanical机械层是定义整个 ...
- 那些年我用过的SAP IDE
在Google上根据关键字"程序员鄙视链"搜索,会得到68多万条结果. 玲琅满目的搜索结果里是众多不同维度划分的鄙视链. 其中有一个维度,就是编程工具的鄙视链,比如: 而我在SAP ...