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 ...
随机推荐
- 基于vue2.0搭建项目流程
搭建vue2.0项目--myproject 一. 环境搭建: 1 打开命令行(cmd) 2 安装node node官网 3 安装 vue-cli步骤如下: npm install -g vue-cli ...
- Linux命令- echo、grep 、重定向、1>&2、2>&1的介绍
最近笔试遇到一道题,关于Linux命令的,题目如下 下面两条命令分别会有怎样的输出 echo hello 1>&2 |grep aaa echo hello 2>&1 ...
- oracle 删除用户,提示“无法删除当前已连接的用户”
1. 首先查询出该用户的登录情况,注意用户名必须是大写 SQL> select username,sid,serial# from v$session where username = 'XST ...
- what is the CCA?
Clear Channel Assessment (CCA) is one of two carrier sense mechanisms in WLAN (or WiFi). It is defin ...
- kubernetes API服务器的安全防护
12.1.了解认证机制 启动API服务器时,通过命令行选项可以开启认证插件. 12.1.1.用户和组 了解用户: 分为两种连接到api服务器的客户端: 1.真实的人 2.pod,使用一种称为Servi ...
- AVL树(查找、插入、删除)——C语言
AVL树 平衡二叉查找树(Self-balancing binary search tree)又被称为AVL树(AVL树是根据它的发明者G. M. Adelson-Velskii和E. M. Land ...
- 重学计算机组成原理(十)- "烫烫烫"乱码的由来
程序 = 算法 + 数据结构 对应到计算机的组成原理(硬件层面) 算法 --- 各种计算机指令 数据结构 --- 二进制数据 计算机用0/1组成的二进制,来表示所有信息 程序指令用到的机器码,是使用二 ...
- Django+zTree构建组织架构树
树,因其清晰明了的展现形式而被广泛的使用 日常的开发过程中我们需要经常与"树"打交道,例如公司的组织架构树.服务器的项目归属树,管理后台侧边树等等,本篇文章介绍关于树的两个内容 多 ...
- LoRaWAN_stack移植笔记(三)__SPI
stm32相关的配置 由于例程使用的主控芯片为STM32L151C8T6,而在本设计中使用的主控芯片为STM32L051C8T6,内核不一样,并且Cube库相关的函数接口及配置也会有不同,所以芯片的驱 ...
- HBase 系列(二)—— HBase 系统架构及数据结构
一.基本概念 一个典型的 Hbase Table 表如下: 1.1 Row Key (行键) Row Key 是用来检索记录的主键.想要访问 HBase Table 中的数据,只有以下三种方式: 通过 ...