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 //如果是的话. //说 ...
随机推荐
- Azure 3月新公布(二)
Azure 3月新发布:HDInsight 的 Apache Hadoop 以及 ExpressRoute 超高性能网关层正式发布,SQL Database Premium RS 层发布公共预览版 A ...
- 洛谷 P1080 国王游戏
题目描述 恰逢 H 国国庆,国王邀请 n 位大臣来玩一个有奖游戏.首先,他让每个大臣在左.右手上面分别写下一个整数,国王自己也在左.右手上各写一个整数.然后,让这 n 位大臣排成一排,国王站在队伍的最 ...
- Udacity carnd2 Sensor Fusion, Extended Karman Filter (English)
Extended Karman Filter Zhenglei 2018 January This is a project to estimate the car position from Lid ...
- 将TIMESTAMP类型的差值转化为秒的方法
两个TIMESTAMP之差得到的是INTERVAL类型,而有时我们只需要得到两个时间相差的秒数,如果变成INTERVAL之后,想要获取这个值会非常麻烦. 比较常见的方法是使用EXTRACT来抽取获得的 ...
- 【CF660E】Different Subsets For All Tuples(组合数学)
点此看题面 大致题意: 有一个长度为\(n\)的数列,每个位置上数字的值在\([1,m]\)范围内,则共有\(m^n\)种可能的数列.分别求出每个数列中本质不同的子序列个数,然后求和. 一些分析 首先 ...
- list 用法的随手记
在list 用法中.1. add是直接添加 一个变量.不能添加一个 集合元素,比如数组 这种写法是错误的 ,因为不能添加集合 这种写法是对的,因为直接添加元素 2. 但是addrannge 是添加一个 ...
- alert、confirm、prompt的区别
我们在开发一些后台管理系统的时候,经常会用到一些弹出框,今天我们一起看一下吧: alert:仅仅是一个提示的作用,通知用户,会阻线程,alert后面的代码,在用户点击确认后仍然会照常执行. confi ...
- 最近的阅读list
fast rcnn 统一了sppnet和rcnn,将原来rcnn分stage的训练合为一个整体的stage,一次完成cls, regression的训练.引入两个loss函数,一个是用来进行cls的, ...
- img适配问题解决方法
将img放到background中,将background-size设置为100%:只需要适配背景为img的元素的宽和高即可.
- Git工作流指南:功能分支工作流(转)
一旦你玩转了集中式工作流,在开发过程中可以很简单地加上功能分支,用来鼓励开发者之间协作和简化交流. 功能分支工作流背后的核心思路是所有的功能开发应该在一个专门的分支,而不是在master分支上.这个隔 ...