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 //如果是的话. //说 ...
随机推荐
- better-scroll 遇到的问题 1
备注:better-scroll 实现下拉,是父子层的结构,父层的第一个子元素,如果超出父容器,那么就可以实现下拉 问题: 今天在使用better-scroll实现下拉功能,遇到了一个问题 &quo ...
- PHP Regex
<?php //Accpet the http client request and generate response content. //As a demo, this function ...
- php的yii框架开发总结5
MVC架构之model类: 我的日报系统用到的数据表:tbl_dailyreport表 其中anthor_id是外键,对应tbl_user数据表的主键id,下面是tbl_user表 class Dai ...
- 还是要精简开发呀,VS2015太大,VS2010不想装
公司电脑配置没有很好,所以对于我就是一个挑战. vs2015装上了,但是一打开就卡卡卡,基本没法办公. 公布能用记事本吧,太多不方便: Notepad++做辅助的局部修改还是很好用的,装上插件就智能提 ...
- Sundy_Android开发深入浅出和高级开发视频教程
Sundy_Android开发深入浅出和高级开发视频教程 放于播音员的网盘中又名:android零基础到高级软件开发工程师培训课程全集(400多讲) 1.课程介绍 2.java重点难点 3.版本控制- ...
- IOS tableView的性能优化(缓存池)
使用缓存池(标识类型) 1.通过 一个 标识 去 缓存池 中寻找可循环得用的cell 2.如果缓存池找不到可循环得用的cell:创建一个新的cell(给cell贴个标识) 3.给cell设置新的数据 ...
- cocos2d-x推断sprite点击
我们经常须要推断用户的点击操作是否落于某个sprite之上,进而让这个sprite做出响应. 可是假设我们通过继承CCSprite类来实现自己的Sprite类的时候,产生的视图尺寸会充满屏幕.多个Sp ...
- 2017.11.16 JavaWeb-------第八章 EL、JSTL、Ajax技术
第八章 EL.JSTL.Ajax技术 ~~ EL (expression language) 是表达式语言 ~~ JSTL(JSP Standard Tag Library) 是开源的JSP标准标签库 ...
- Yarn下Map数控制
public List<InputSplit> getSplits(JobContext job) throws IOException { long minSize = Math.max ...
- lintcode_397_最长上升连续子序列
最长上升连续子序列 描述 笔记 数据 评测 给定一个整数数组(下标从 0 到 n-1, n 表示整个数组的规模),请找出该数组中的最长上升连续子序列.(最长上升连续子序列可以定义为从右到左或从左到 ...