【11.61%】【codeforces 670F】Restore a Number
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的更多相关文章
- .NET周报【11月第1期 2022-11-07】
国内文章 开源·安全·赋能 - .NET Conf China 2022 https://mp.weixin.qq.com/s/_tYpfPeQgyEGsnR4vVLzHg .NET Conf Chi ...
- 【 BowWow and the Timetable CodeForces - 1204A 】【思维】
题目链接 可以发现 十进制4 对应 二进制100 十进制16 对应 二进制10000 十进制64 对应 二进制1000000 可以发现每多两个零,4的次幂就增加1. 用string读入题目给定的二进制 ...
- 【codeforces 709D】Recover the String
[题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...
- 【codeforces 776E】The Holmes Children
[题目链接]:http://codeforces.com/contest/776/problem/E [题意] f(n)是小于n的不同整数对(x,y)这里x+y==n且gcd(x,y)==1的个数; ...
- 【codeforces 727D】T-shirts Distribution
[题目链接]:http://codeforces.com/problemset/problem/727/D [题意] 给你6种尺寸的衣服; 他们的尺码依次为S, M, L, XL, XXL, XXXL ...
- 【codeforces 794B】Cutting Carrot
[题目链接]:http://codeforces.com/contest/794/problem/B [题意] 给你一个等腰三角形; 它的底边为1; 高为h; 要求你把这个等腰三角形分成n份面积相等的 ...
- 【codeforces 799D】Field expansion
[题目链接]:http://codeforces.com/contest/799/problem/D [题意] 给你长方形的两条边h,w; 你每次可以从n个数字中选出一个数字x; 然后把h或w乘上x; ...
- 【codeforces 589G】Hiring
[题目链接]:http://codeforces.com/problemset/problem/589/G [题意] 有n个人; 每个人每天在开始工作之前,都需要di单位的准备时间,然后才能开始工作; ...
- 【42.86%】【Codeforces Round #380D】Sea Battle
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
随机推荐
- bzoj1179 Atm
Description Input 第一行包含两个整数N.M.N表示路口的个数,M表示道路条数.接下来M行,每行两个整数,这两个整数都在1到N之间,第i+1行的两个整数表示第i条道路的起点和终点的路口 ...
- hdu4313 贪心+并查集
题意简单思路也还可以.开始从小到大排序非常烦.后来从大到小就很简单了: 从大到小解决了删除的边最小. #include<stdio.h> #include<string.h> ...
- SpringMVC的简单传值总结
之前学习SpringMVC时感觉他的传值很神奇:简便,快捷,高效. 今天写几个简单的传值与大家分享,希望能对大家有帮助. 一. 从后往前传: (1) @Controller @RequestMappi ...
- 通过反射 往泛型Integer的集合里添加String 类型的数据 Day25
package com.sxt.method1; import java.lang.reflect.Method; /* * 需求:通过反射 往泛型Integer的集合里添加String 类型的数据 ...
- CNN对位移、尺度和旋转不变性的讨论
CNN得益于全局共享权值和pool操作,具有平移不变性. 对于尺度不变性,是没有或者说具有一定的不变性(尺度变化不大),实验中小目标的检测是难点,需要采用FPN或者其他的方式单独处理. 对于旋转不变性 ...
- du,df区别
1.记住命令 du:disk Usage -h, --human-readable print sizes in human readable format df:disk free 2.区别 du ...
- 【BootStrap】--具有增删改查功能的表格Demo
[BootStrap]--具有增删改查功能的表格Demo 目录(?)[+] 前言 版本一 样式 代码 版本二 样式 代码 版本三 样式 代码 总结 前言 bootstrap的表格样式,有类似EasyU ...
- @loj - 2090@ 「ZJOI2016」旅行者
目录 @description@ @solution@ @accepted code@ @details@ @description@ 小 Y 来到了一个新的城市旅行.她发现了这个城市的布局是网格状的 ...
- Flask学习之十一 邮件支持
英文博客地址:blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-xi-email-support 中文翻译地址:http://www. ...
- Facebook F8|闲鱼高级技术专家参会分享
笔者代表闲鱼参加了Facebook在4月30日举行的为期二天的F8大会,地点加州.将会议概括和一些收获分享给大家.对国内开发者而言,Facebook的产品设计.社区.VR/AR等有一些借鉴意义:对海外 ...