cf Permute Digits(dfs)
C. 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.
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.
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.
123
222
213
3921
10000
9321
4940
5000
4940
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std; int f, la, lb, vis[];
char a[], b[], ans[]; void dfs(int dep, int flag)//参数:当前位,结果当前位是否比 b小 ,dep是搜索深度
{
if (f == ) return; //如果已经找到结果则退出
if (dep == la)
{ //找到结果,将标志 f=1,并退出
f = ;
return;
}
for (int i = ; i >= ; i--)
{ //从大到小扫描数字
if (vis[i])//如果有可用数字 ,数组中0~9出现的次数不为零
{
if (flag || b[dep] == i + '')//如果已经出现某一位比 b小的或这当前位相等
{
vis[i]--;
ans[dep] = i + ''; //记录结果
dfs(dep + , flag); //处理下一个位置
if (f) return; //如果找到结果则退出
vis[i]++; //回溯时还原,重新处理当前位
}
else if (b[dep]>i + '')
{ //结果的当前位比 b小
vis[i]--;
ans[dep] = i + ''; //记录结果
dfs(dep + , ); //处理下一位,并将 flag=1
if (f) return; //如果找到结果则退出
//vis[i]++; //由于这个分支只可能进入一次,不还原也可以
}
}
}
}
int main()
{
int i;
while (cin >> a >> b)
{
la = strlen(a);
lb = strlen(b);
if (la<lb)
{ //当位数不同时
sort(a, a + la);
for (i = la - ; i >= ; i--)
cout << a[i];
cout << endl;
}
else
{ //当位数相同时
f = ;
memset(vis, , sizeof(vis));
for (i = ; i<la; i++)
vis[a[i] - '']++; //统计 a中每个数出现的次数
dfs(, ); //深搜
for (i = ; i<la; i++)
cout << ans[i];
cout << endl;
}
}
return ;
}
cf Permute Digits(dfs)的更多相关文章
- C. Permute Digits dfs大模拟
http://codeforces.com/contest/915/problem/C 这题麻烦在前导0可以直接删除,比如 1001 100 应该输出11就好 我的做法是用dfs,每一位每一位的比较. ...
- 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 ...
- Permute Digits 915C
You are given two positive integer numbers a and b. Permute (change order) of the digits of a to con ...
- poj 3373 Changing Digits (DFS + 记忆化剪枝+鸽巢原理思想)
http://poj.org/problem?id=3373 Changing Digits Time Limit: 3000MS Memory Limit: 65536K Total Submi ...
- 【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 ...
- CF Preparing Olympiad (DFS)
Preparing Olympiad time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
随机推荐
- ORACLE体系结构一 (实例(instance))--ORACLE_SID
数据库实例(也称为服务器Server)就是用来访问一个数据库文件集的一个存储结构及后台进程的集合.它使一个单独的数据库可以被多个实例访问(也就是ORACLE并行服务器-- OPS).实例在操作系统中用 ...
- onRetainNonConfigurationInstance方法状态保存
onRetainNonConfigurationInstance方法作用于ONSAVEINSTANCE类似,但是能保存更多的信息,可以使用getLastNonConfigurationInstance ...
- 3-2 zk客户端连接关闭服务端,查看znode
使用ZooKeeper官方提供的Client来连接.路径类似的结构. 连接到我们的门户HOST. quota属于zookeeper.quota是子节点,zookeeper是父节点.quota其实是一个 ...
- day17 13.滚动结果集介绍
滚动 一般结果集只能是向下的,不是滚动的,你要是想让它滚动你得设置才行. 类名或者接口里面有静态的可以.接口里面的属性全部都是public static final,类名/接口名.是属性,这些都是常量 ...
- [chmod]linux中给文件增加权限
chmod命令 1.chmod u+x file.sh 2.sudo chmod 777 文件名 注: 如果给所有人添加可执行权限:chmod a+x 文件名:如果给文件所有者添加可执行权限:chm ...
- TortoiseSVN客户端安装遇到的问题汇总
在windows server 2003版本上安装32位SVN客户,提示以下错误 1:无法通过windows installer服务安装此安装程序包” 这时需要安装更新的windows install ...
- Struts2 看1
Struts2部分 1. JavaEE软件三层结构和MVC的区别? JavaEE软件三层机构是由sun公司提供JavaEE开发规范的:Web层(表现层).业务逻辑层.数据持久层.[其中WEB层会使用前 ...
- VMware Workstation 软件 创建 Ubuntu 14.04虚拟机
VMware Workstation 软件 创建 Ubuntu 14.04虚拟机 1. 安装VMare Workstation 软件 下载VMware 11 软件 http://www.dntk.or ...
- Blender 工具使用——模式切换
Blender 工具使用--模式切换 制作骨架时 在物件模式(Object Mode)下使用鼠标右键选中一个骨架,按Tab键,可以切换为编辑模式(Edit Mode),按Ctrl + Tab可以进入骨 ...
- 10.model/view实例(3)
任务:3x2的表格,第一个单元格显示当前时间 思考: 1.data函数里面QTime::currentTime()显示当前时间 2.但是这个事件是一个固定的时间,不会变动 3.需要时间变动,View就 ...