【11.61%】【codeforces 670F】Restore a Number

时间:2021-05-21 13:06:58

time limit per test2 seconds

memory limit per test256 megabytes

inputstandard input

outputstandard output

Vasya decided to pass a very large integer n to Kate. First, he wrote that number as a string, then he appended to the right integer k — the number of digits in n.

Magically, all the numbers were shuffled in arbitrary order while this note was passed to Kate. The only thing that Vasya remembers, is a non-empty substring of n (a substring of n is a sequence of consecutive digits of the number n).

Vasya knows that there may be more than one way to restore the number n. Your task is to find the smallest possible initial integer n. Note that decimal representation of number n contained no leading zeroes, except the case the integer n was equal to zero itself (in this case a single digit 0 was used).

Input

The first line of the input contains the string received by Kate. The number of digits in this string does not exceed 1 000 000.

The second line contains the substring of n which Vasya remembers. This string can contain leading zeroes.

It is guaranteed that the input data is correct, and the answer always exists.

Output

Print the smalles integer n which Vasya could pass to Kate.

Examples

input

003512

021

output

30021

input

199966633300

63

output

3036366999

【题解】



怎样从所给的序列中求出原来数字的长度?

比如给的序列长度为93;

则很容易想到实际位数为91,然后最后面加上了数字92,变成长度为93;

又比如103位,则100是实际长度,最后加上数字100,然后就变成103位了。

1004位则,1000位,后面加上1000这个数字占了4位。

然后最多的位数为1000000;

则枚举描述这个数位需要的数字个数i;

设g(x)为某个数字的长度;

所给序列的长度为len;



if (g(len-i)==i) ->len-i就是原来的数字的长度;

把len-i这个数字的各个位上的数字在所给的数字中去掉

然后所给的子串是连续的几个数字

也在原数中把它剔除掉;

最后剩下的数字分两类搞;

ans1:找一个最小的非0数字放在最前面。剩下的升序加入。然后在加入的时候判断所给的数字应该放在哪里;

ans2:直接把那个子串放在最前面;剩余的升序放;

ans2可以包括最后答案等0的情况;

当然ans1和ans2也可以合在一起写;

最后输出min(ans1,ans2)即可;

#include <cstdio>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
#include <stack>
#include <string>
#define LL long long using namespace std; const int MAXN = 1000010; char s1[MAXN], s2[MAXN];
int cnt[10]; void input_ll(LL &r)
{
r = 0;
char t = getchar();
while (!isdigit(t)) t = getchar();
LL sign = 1;
if (t == '-')sign = -1;
while (!isdigit(t)) t = getchar();
while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
r = r*sign;
} void input_int(int &r)
{
r = 0;
char t = getchar();
while (!isdigit(t)) t = getchar();
int sign = 1;
if (t == '-')sign = -1;
while (!isdigit(t)) t = getchar();
while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
r = r*sign;
} bool ok(char t,int len)
{
for (int i = 1; i <= len; i++)
if (s2[i] != t)
return t > s2[i];
return false;
} int main()
{
//freopen("F:\\rush.txt", "r", stdin);
scanf("%s", s1+1);
int len = strlen(s1 + 1);
for (int i = 1; i <= len; i++)
cnt[s1[i] - '0']++; int nw;
for (int i = 1; i <= 7; i++)
{
int num = len - i;
int tt = 0;
while (num)
{
tt++;
num /= 10;
}
if (tt == i)
{
nw = len-i;
break;
}
}
while (nw)
{
cnt[nw % 10]--;
nw /= 10;
} scanf("%s", s2 + 1);
len = strlen(s2 + 1);
for (int i = 1; i <= len; i++)
cnt[s2[i] - '0']--; int findnum = -1;
bool added = false;
string ans1="", ans2="";
ans2 += s2 + 1; for (int i=1;i <= 9;i++)
if (cnt[i] > 0)
{
ans1 += char(i + '0');
findnum = i;
cnt[i]--;
break;
} for (int i = 0; i <= 9; i++)
{
char t = i + '0';
if (findnum == i)
ans2 += t;
if (!added && ok(t,len))
{
ans1 += s2 + 1;
added = true;
}
while (cnt[i])
{
ans1 += t;
ans2 += t;
cnt[i]--;
}
}
if (findnum == -1)
puts(ans2.c_str());
else
{
if (!added)
ans1 += s2;
if (ans2[0] == '0')
puts(ans1.c_str());
else
puts(min(ans1, ans2).c_str());
}
return 0;
}

【11.61%】【codeforces 670F】Restore a Number的更多相关文章

  1. 【 BowWow and the Timetable CodeForces - 1204A 】【思维】

    题目链接 可以发现 十进制4 对应 二进制100 十进制16 对应 二进制10000 十进制64 对应 二进制1000000 可以发现每多两个零,4的次幂就增加1. 用string读入题目给定的二进制 ...

  2. 【codeforces 709D】Recover the String

    [题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...

  3. 【codeforces 776E】The Holmes Children

    [题目链接]:http://codeforces.com/contest/776/problem/E [题意] f(n)是小于n的不同整数对(x,y)这里x+y==n且gcd(x,y)==1的个数; ...

  4. 【codeforces 727D】T-shirts Distribution

    [题目链接]:http://codeforces.com/problemset/problem/727/D [题意] 给你6种尺寸的衣服; 他们的尺码依次为S, M, L, XL, XXL, XXXL ...

  5. 【codeforces 794B】Cutting Carrot

    [题目链接]:http://codeforces.com/contest/794/problem/B [题意] 给你一个等腰三角形; 它的底边为1; 高为h; 要求你把这个等腰三角形分成n份面积相等的 ...

  6. 【codeforces 799D】Field expansion

    [题目链接]:http://codeforces.com/contest/799/problem/D [题意] 给你长方形的两条边h,w; 你每次可以从n个数字中选出一个数字x; 然后把h或w乘上x; ...

  7. 【codeforces 589G】Hiring

    [题目链接]:http://codeforces.com/problemset/problem/589/G [题意] 有n个人; 每个人每天在开始工作之前,都需要di单位的准备时间,然后才能开始工作; ...

  8. 【42&period;86&percnt;】【Codeforces Round &num;380D】Sea Battle

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  9. 【26&period;83&percnt;】【Codeforces Round &num;380C】Road to Cinema

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

随机推荐

  1. ionic 开发APP 安装配置详解以及 cordova 环境配置详细过程

    整个安装过程:     1. jdk 1.7.2   (http://www.oracle.com/technetwork/java/javase/downloads/index.html) 安装好之 ...

  2. C&num; 禁止程序多个实例运行

    //program.cs    static class Program    {        /// <summary>        /// 应用程序的主入口点.        // ...

  3. kali无法输入中文

    已经装了fcitx,之前都是可以用的,今天启动时发现无法切换出中文输入法 用 Fcitx config tool查看发现输入法表里面是空的 最后发现是启动时fcitx进程没自动运行,加入自动运行后重启 ...

  4. qq2440启动linux后出现错误提示request&lowbar;module&colon; runaway loop modprobe binfmt-464c

    1.情景: 编译busybox时加了make CROSS_COMPILE=arm-linux-,但是还是出现了此情况! 2.解决方案如下: 配置busybox时,在配置中发现busybox setti ...

  5. JavaScript性能优化之函数节流(throttle)与函数去抖(debounce)

    函数节流,简单地讲,就是让一个函数无法在很短的时间间隔内连续调用,只有当上一次函数执行后过了你规定的时间间隔,才能进行下一次该函数的调用. 函数节流的原理挺简单的,估计大家都想到了,那就是定时器.当我 ...

  6. 【一天一道LeetCode】&num;169&period; Majority Element

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  7. nnet3配置中的上下文和chunk(块)大小

    Nnet3配置中的上下文和块大小 简介 本页讨论了nnet3配置中关于解码和训练的块大小以及左右上下文的某些术语.这将有助于理解一些脚本.目前,从脚本角度来看,没有任何关于nnet3的"概述 ...

  8. c&plus;&plus; 单元测试框架 gmock 深度剖析

    c++ 单元测试框架 gmock 深度剖析 随着微服务和CI的流行,在目前的软件工程领域中单元测试可以说是必不可少的一个环节,在TDD中,单元测试更是被提高到了一个新的高度.但是很多公司由于很多不同的 ...

  9. 【awk】提取文件第一列

    生信数据文件一般是按列分开的,如果我们只想简单的提取一列而不是费尽周折写个程序提取哪一列的话,awk作为一个非常好用的文档处理工具,我们现在来简单看一下他的一些功能: awk '{print $1}' ...

  10. 解决invalid record found in VCF4 file &lpar;at least 8 tab-delimited fields expected&rpar;问题,批量修改空格改为制表格格式

    出现这种问题说明一般存在两个问题: 第一,vcf文件不足8个分割制表符,比如像如下文件: 为了解决这个问题,说明在做snp filter时候,需要提取至少8个制表符的字符串,比如,像如下文件所示: 第 ...