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 ...
随机推荐
- 剑指offer 39_二叉树的深度
#include <stdio.h> #include <malloc.h> typedef int Item; typedef struct node{ Item m_val ...
- day70-oracle 12-触发器
查询是没有触发器的.trigger是一个数据库的对象.PL/SQL程序是在我插入之前执行还是在插入之后执行?触发器类似于java中的监听器. 监听插入操作,执行一段PLSQL程序. 禁止在非工作时间插 ...
- 洛谷P3328(bzoj 4085)毒瘤线段树
题面及大致思路:https://www.cnblogs.com/Yangrui-Blog/p/9623294.html, https://www.cnblogs.com/New-Godess/p/45 ...
- CodeForces 1109C. Sasha and a Patient Friend
题目简述:维护以下三种操作 1. "1 t s":在时刻$t$插入命令$s$.保证任意操作后,任意时刻至多只有一个命令. 2. "2 t":删除时刻$t$的命令 ...
- UIScrollView现实循环滚动
#import "RootViewController.h" #define width [UIScreen mainScreen].bounds.size.width #defi ...
- ROS Learning-005 beginner_Tutorials 创建ROS程序包(就是软件包)
ROS Indigo beginner_Tutorials-04 创建ROS程序包(就是软件包) 我使用的虚拟机软件:VMware Workstation 11 使用的Ubuntu系统:Ubuntu ...
- 红帽rhel7.1usbguard
https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html/security_guide/sec-usi ...
- Entity Framework Tutorial Basics(39):Raw SQL Query
Execute Native SQL Query You can execute native raw SQL query against the database using DBContext. ...
- SDUT 3373 数据结构实验之查找一:二叉排序树
数据结构实验之查找一:二叉排序树 Time Limit: 400MS Memory Limit: 65536KB Submit Statistic Problem Description 对应给定的一 ...
- 20169206 2016-2017-2 《网络攻防实践》 nmap的使用
Part I 使用nmap扫描ubuntu靶机 先给出nmap的官方中文操作手册https://nmap.org/man/zh/,其实并不太好用,而且有时候会打不开,但至少是官方手册. 探查操作系统 ...