CodeForces - 95B
Petya loves lucky numbers. Everybody knows that positive integers are lucky if their decimal representation doesn't contain digits other than 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
Lucky number is super lucky if it's decimal representation contains equal amount of digits 4 and 7. For example, numbers 47, 7744, 474477 are super lucky and 4, 744, 467 are not.
One day Petya came across a positive integer n. Help him to find the least super lucky number which is not less than n.
Input
The only line contains a positive integer n (1 ≤ n ≤ 10100000). This number doesn't have leading zeroes.
Output
Output the least super lucky number that is more than or equal to n.
Examples
4500
4747
47
47 题目大意:输入一个正整数,求不小与这个数的最小的超级幸运数(超级幸运数是4和7的个数相同的数,并且只有4和7)
思路:这道题dfs能过真是吓到我了,数据这么大,这也能过只能说数据有点水,思路看代码吧
#include<iostream>
#include<string.h>
#include<map>
#include<cstdio>
#include<cstring>
#include<stdio.h>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<set>
#include<queue>
typedef long long ll;
using namespace std;
const ll mod=1e9+;
const int maxn=1e6+;
const int maxk=5e3+;
const int maxx=1e4+;
const ll maxe=+;
#define INF 0x3f3f3f3f3f3f
#define Lson l,mid,rt<<1
#define Rson mid+1,r,rt<<1|1
char a[maxn],ans[maxn];
int len;
bool dfs(int pol,int sum1,int sum2,bool limit)//当前位,4的个数,7的个数,是否已经没有限制了
{
if(pol>=len) return true;
if(limit)//没有限制了,那么直接把剩余的4和7放进去就可以了
{
for(int i=;i<sum1;i++) ans[pol++]='';
for(int i=;i<sum2;i++) ans[pol++]='';
return true;
}
if(sum1&&a[pol]<='')
{
if(dfs(pol+,sum1-,sum2,a[pol]!=''))
{
ans[pol]='';
return true;
}
}
if(sum2&&a[pol]<='')
{
if(dfs(pol+,sum1,sum2-,a[pol]!=''))
{
ans[pol]='';
return true;
}
}
return false;//4和7都不能选,那么代表要加两位数了
}
int main()
{
cin>>a;
len=strlen(a);
if(len&||!dfs(,len/,len/,))//如果长度是奇数的话直接加一位数,前面的是4后面的是7,判断偶数的时候是否有满足条件的数
{
if(len&) len++;
else len+=;
int i;
for(i=;i<len/;i++) ans[i]='';
for(i;i<len;i++) ans[i]='';
}
cout<<ans<<endl;
return ;
}
CodeForces - 95B的更多相关文章
- CodeForces - 95B(DFS)
题目链接:http://codeforces.com/problemset/problem/95/B 题目大意:给你一个正整数n (1 ≤ n ≤ 10100000),求不大小于它的超级幸运数字(超级 ...
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- 【Codeforces 738A】Interview with Oleg
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
- CodeForces - 662A Gambling Nim
http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...
- CodeForces - 274B Zero Tree
http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...
- CodeForces - 261B Maxim and Restaurant
http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...
- CodeForces - 696B Puzzles
http://codeforces.com/problemset/problem/696/B 题目大意: 这是一颗有n个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...
随机推荐
- 安装pyenv版本管理
系统:Centos7.4 安装pyenv是为了更好的管理python的版本. 在进行安装操作之前,首先使用普通用户test,进行操作,如下: #安装之前先安装依赖的库 [test@localhost ...
- React中state和props的区别
props和state都是用于描述component状态的,并且这个状态应该是与显示相关的. State 如果component的某些状态需要被改变,并且会影响到component的render,那么 ...
- ServerSocket01
ServerSocket表示服务端套接字:我们首先来看下其中的一个构造器: public ServerSocket(int port,int backlog) throws IOException 其 ...
- 使用Timer为界面执行异步任务
-------------------siwuxie095 工程名:TestSwingTimer 包名:com.siwuxie095.timer 类名: ...
- Angular13 Angular2发送PUT请求在后台接收不到参数
1 问题描述 利用angular2发送PUT请求时,后端接收不到参数 2 问题诊断 前段参数格式问题,后端获取参数的方法不对 3 解决问题 angular前段:将所有参数编程JSON字符串形式 spr ...
- mac 创建快捷方式
一.替身文件 mac上有个功能叫“制作替身”.使用 ⌘+⇧+鼠标左键拖拽 可以生成一个文件的替身文件.此替身文件相当于windows的快捷方式. 二.link文件 link文件也可以做快捷方式.还 ...
- Ubuntu12.04安装R ,Rstudio, RHive
环境: Ubuntu12.04 R-3.1.0 0.Ubuntu安装R官网的介绍 http://mirrors.ustc.edu.cn/CRAN/ Precise Pangolin (12.04; L ...
- win7 32位安装 python 及Numpy、scipy、matplotlib函数包
操作系统: win7 64位,但选择安装32位的python. 1,python下载安装 https://www.python.org/downloads/ 下载2.7版,一路下一步安装. 并在pat ...
- day1_2_3
DD烧写命令(mfgtools-without-rootfs.tar.gz) ubuntu minicom svn 应用层进程阻塞调试 多机共享 securecrt的远程登录以及调试 tengxunt ...
- Struts2学习第六课 实现登录登出功能
关于Struts2请求的扩展名问题: 1).org.apache.struts2包下的default.properties中配置了struts2应用的一些常量 2).struts.action.ext ...