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

Input
4500
Output
4747
Input
47
Output
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的更多相关文章

  1. CodeForces - 95B(DFS)

    题目链接:http://codeforces.com/problemset/problem/95/B 题目大意:给你一个正整数n (1 ≤ n ≤ 10100000),求不大小于它的超级幸运数字(超级 ...

  2. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  3. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  4. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  5. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

  6. CodeForces - 662A Gambling Nim

    http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...

  7. CodeForces - 274B Zero Tree

    http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...

  8. CodeForces - 261B Maxim and Restaurant

    http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...

  9. CodeForces - 696B Puzzles

    http://codeforces.com/problemset/problem/696/B 题目大意: 这是一颗有n个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...

随机推荐

  1. Poj 2853,2140 Sequence Sum Possibilities(因式分解)

    一.Description Most positive integers may be written as a sum of a sequence of at least two consecuti ...

  2. 转载:PLSQL Developer使用技巧整理

    Shortcut(快捷方式): Edit/Undo     Ctrl+Z Edit/Redo     Shift+Ctrl+Z Edit/PL/SQL Beautifier  Ctrl+W   (自定 ...

  3. LAMP 1.9域名301跳转

    给两个域名分主次.输入次域名跳转到主域名然后进行访问. 首先打开虚拟机配置文件. vim /usr/local/apache2/conf/extra/httpd-vhosts.conf 把这段配置添加 ...

  4. Material使用10 MdRadioModule、MdDatepickerModule、MdNativeDateModule、MdSelectModule

    1 MdRadioModule 相当于<input type="radio"> 2 使用步骤 2.1 在共享模块导入MdRadioModule import { NgM ...

  5. Learning Python 005 字符串和编码

    Python 字符串和编码 介绍 计算机是美国人发明的,最早只有127个字母被编码到计算机,这个编码表被称为ASCII编码,比如大写字母A的编码是65,小写字母z的编码是122. 处理中文一个字节显然 ...

  6. VCF文件处理工具PyVCF

    vcf格式示例 ##fileformat=VCFv4.1 ##FILTER=<ID=LowQual,Description=”Low quality”> ##FORMAT=<ID=A ...

  7. 7.21实习培训日志-JDBC JSP Servlet

    JDBC JSP Servlet 总结 今天早上的考试主要考Java的网络,HttpClient的get,post方法,Socket的文件传输和Xml和Json文件的解析,对于HttpCLient很简 ...

  8. SuperSocket框架中BinaryRequestInfo协议的使用

    一.开发环境 1.Windows 10 企业版 64位 2.Microsoft Visual Studio 2017 企业版 二.项目开始 1.新建控制台程序,项目名称“BinarySuperSock ...

  9. C# 将数组转换为以逗号分隔的字符串

    例子: string[] array = { "A", "B", "C", "D" }; string str = st ...

  10. [CentOS7] Segmentation fault (core dumped),但是在主机上找不到core文件

    1.问题描述 程序执行报:Segmentation fault (core dumped),但是在主机上找不到core文件 2.如何让系统生成core file /home>ulimit -ac ...