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 ...
随机推荐
- MyBatis总结三:使用动态代理实现dao接口
由于我们上一篇实现MyBatis的增删改查的接口实现类的方法都是通过sqlsession调用方法,参数也都类似,所以我们使用动态代理的方式来完善这一点 MyBatis动态代理生成dao的步骤: 编写数 ...
- elasticsearch配置文件里的一些坑 [Failed to load settings from [elasticsearch.yml]]
这里整理几个空格引起的问题. 版本是elasticsearch-2.3.0 或者elasticsearch-rtf-master Exception in thread "main" ...
- Linux,du、df统计的硬盘使用情况不一致问题
[转]http://blog.linezing.com/?p=2136 Linux,du.df统计的硬盘使用情况不一致问题 在运维Linux服务器时,会碰到需要查看硬盘空间的情况,这时候,通常会使 ...
- UTF8转unicode说明
1.最新版iconv中的char *encTo = "UNICODE//IGNORE"; 是没有这个字符串的,它里面有UNICODELITTLE 和 UNICODEBIG 而且是没 ...
- Win10 VS2013 PCL1.8.1和依赖项VTK8.0.1, QHuall(2.15.2), FLANN1.9.1,Boost1.59.0,Zbil1.2.11和libPNG1.6.34编译安装
编译和安装过程最好使用管理员权限去操作,避免不必要的错误. 一般而言为了区分Debug和Release库,添加输入变量 Name: CMAKE_DEBUG_POSTFIX Type: STRING V ...
- Linux脚本设计4——一些实用程序
实用程序1:列目录 #!/bin/bash path=`echo $PATH | sed 's/:/ /g'` for d in $path do echo $d done 这是一个for循环,注意p ...
- 连接Excel数据库
SQL语法:http://www.w3school.com.cn/sql/sql_syntax.asp Ctrl键拖(也就是复制) 先输入1,2,然后下拉 一.问题的提出 在ASP编程中会遇到很多大大 ...
- spring 中配置sessionFactory及用法
spring 中配置sessionFactory及用法 方法一: 1.在Spring的applicationContext.xml中配置bean <!-- 启用注解注入 --> ...
- mono-3.0.2安装指南
install-mono.sh.zip mono-3.0.2安装指南.pdf mod_mono.diff.zip mono-3.0.2安装指南 一见 2012/12/27 目录 1. 前言 ...
- angular 分页2
http://www.alliedjeep.com/2547.htm AngularJS Code (Users.js) var Users = angular.module('Users', []) ...