CF915C Permute Digits 字符串 贪心
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 题意:给你两个小于10^18的数a,b,a的每一位的数可以随意排列,求所得到的小于b的最大a
分析:每次暴力判断每一位数放前面时后面取最大是否大于b,如果不大于则当前位取这个数是最佳
AC代码:
#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <string>
#include <bitset>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <algorithm>
#include <bits/stdc++.h>
#define ls (r<<1)
#define rs (r<<1|1)
#define debug(a) cout << #a << " " << a << endl
using namespace std;
typedef long long ll;
const ll maxn = 1e4+10;
const ll mod = 1000000007;
const double pi = acos(-1.0);
const double eps = 1e-8;
bool cmp( char p, char q ) {
return p > q;
}
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
string s1, s2;
while( cin >> s1 >> s2 ) {
ll len1 = s1.length(), len2 = s2.length();
sort(s1.begin(),s1.end());
if( len1 < len2 ) {
reverse(s1.begin(),s1.end());
cout << s1 << endl;
continue;
}
for( ll i = 0; i < len1; i ++ ) {
for( ll j = len1-1; j > i; j -- ) {
string t = s1;
swap(s1[i],s1[j]);
sort(s1.begin()+i+1,s1.end());
if( s1 > s2 ) {
s1 = t; //每次循环确定一个当前所能取到的最小值中的最大值数字
} else {
break;
}
}
//debug(s1);
}
cout << s1 << endl;
}
return 0;
}
CF915C Permute Digits 字符串 贪心的更多相关文章
- CF915C Permute Digits
思路: 从左到右贪心放置数字,要注意判断这个数字能否放置在当前位. 实现: #include <bits/stdc++.h> using namespace std; typedef lo ...
- Codeforces 915 C. Permute Digits (dfs)
题目链接:Permute Digits 题意: 给出了两个数字a,b(<=1e18),保证a,b都不带前缀0.用a的字符重组一个数字使这个值最大且小于b.(保证这个值存在) 题解: 这题遇到了不 ...
- CodeForces-915C Permute Digits
C. Permute Digits time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- cf Permute Digits(dfs)
C. Permute Digits You are given two positive integer numbers a and b. Permute (change order) of the ...
- CodeForces 489C Given Length and Sum of Digits... (贪心)
Given Length and Sum of Digits... 题目链接: http://acm.hust.edu.cn/vjudge/contest/121332#problem/F Descr ...
- 【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 ...
- Permute Digits 915C
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 ...
- 【每日一题】UVA - 1368 DNA Consensus String 字符串+贪心+阅读题
https://cn.vjudge.net/problem/UVA-1368 二维的hamming距离算法: For binary strings a and b the Hamming distan ...
随机推荐
- 使用f12定位bug
为什么找到网站中的bug后还要去分析它到底是属于前端bug还是后端bug 三个原因: 1.在一些公司,一个系统可能是由前端团队和后端团队共同开发出来的,因此在分配bug的时候,不同模块的bug一般都会 ...
- F#周报2019年第31期
新闻 现在开始接受FSSF的第七次师友计划申请 Xamarin播客:XAML热重载 TorchSharp:将PyTorch引擎带入.NET 视频及幻灯片 F#中的异步编程2/3--实现异步工作流 ML ...
- str_replace导致的注入问题汇总
研究了下replace的注入安全问题. 一般sql注入的过滤方式就是引用addslashes函数进行过滤. 他会把注入的单引号转换成\',把双引号转换成\",反斜杠会转换成\\等 写一段ph ...
- c#异常后重试操作
private void TryConnect(System.Action action) { int retries = 3; whi ...
- 全球十大OTA 谁能有一席之地?
全球十大OTA 谁能有一席之地? http://www.traveldaily.cn/article/78381/1 2014-03-05 来源:i黑马 随着旅游行业日新月异的发展,在线旅游网站的出现 ...
- element ui 退出功能
<template> <el-container class="home-wrapper"> <el-header> <el-row ty ...
- 本地(任意)时间戳转化(转换)标准时间格式 js(eg:2019-05-07 17:49:12)
<script> function getLocalTime(timestamp) { // 如果以秒为单位 // var dateObj = new Date(timestamp * 1 ...
- javaScript基础-02 javascript表达式和运算符
一.原始表达式 原始表达式是表达式的最小单位,不再包含其他表达式,包含常量,直接量,关键字和变量. 二.对象和数组的初始化表达式 对象和数组初始化表达式实际上是一个新创建的对象和数组. 三.函数表达式 ...
- 【Python3爬虫】学习分布式爬虫第一步--Redis分布式爬虫初体验
一.写在前面 之前写的爬虫都是单机爬虫,还没有尝试过分布式爬虫,这次就是一个分布式爬虫的初体验.所谓分布式爬虫,就是要用多台电脑同时爬取数据,相比于单机爬虫,分布式爬虫的爬取速度更快,也能更好地应对I ...
- springboot脚手架,逐渐成长成一个优秀的开源框架
目录 项目介绍 环境搭建 开发工具 开发环境 工具安装 系统架构 启动项目 springboot基于spring和mvc做了很多默认的封装.这样做的好处极大的方便了开发者的效率.尽管与此我们每个人还是 ...