【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 ...
随机推荐
- framework7 上拉加载一些ajax问题
1.请求第一组数据后如果不能产生上拉进度条,则无法进行上拉加载. 解决办法:首次加载的数据量设置合理即可. 2.同一组数据请求多次,原因是异步刷新时间差,请求参数未更新,多次触发了上拉加载. 解决办法 ...
- 转载 LibGDX: 使用 Gradle 命令运行和打包项目
版权声明:本文为csdn xietansheng 博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接: http://blog.csdn.net/xieta ...
- idea java内存分析工具
https://blog.csdn.net/qq_22194659/article/details/83829891 https://www.ej-technologies.com/products/ ...
- 在springmvc中 @RequestMapping(value={"", "/"})是什么意思
这个意思是说请求路径 可以为空或者/ 我给你举个例子:比如百度知道的个人中心 访问路径是 http://zhidao.baidu.com/ihome,当然你也可以通过 http://zhidao.ba ...
- oracle函数 extract(c1 from d1)
[功能]:日期/时间d1中,参数(c1)的值 [参数]:d1日期型(date)/日期时间型(timestamp),c1为字符型(参数) [参数表]:c1对应的参数表详见示例 [返回]:字符 [示例] ...
- 「HNOI2015」菜肴制作
「HNOI2015」菜肴制作 这道题想到了其实还挺水的,一开始我直接用小根堆拓扑然后就爆0了,然后我又用十万个堆搜索,T30,还是xkl告诉我要倒着拓扑. 首先要建反图,对于入度为0的点,较小的点先输 ...
- TOP10!全球顶级云计算公司战斗力排行榜
TOP10!全球顶级云计算公司战斗力排行榜 1亚马逊\VMware.微软 [PConline 资讯]现如今,不谈“云”,似乎会与这个时代格格不入.无论是企业还是个人,都会与“云”扯上关系.可以说,云计 ...
- Pytorch实现MNIST(附SGD、Adam、AdaBound不同优化器下的训练比较) adabound实现
学习工具最快的方法就是在使用的过程中学习,也就是在工作中(解决实际问题中)学习.文章结尾处附完整代码. 一.数据准备 在Pytorch中提供了MNIST的数据,因此我们只需要使用Pytorch提供 ...
- hdu 1289 Hat’s IEEE
Problem - 1289 好题.其实就是模拟IEEE754的格式,不过要注意的是,这里用的32位是float,用double就不对了. 代码如下: #include <cstdio> ...
- ]ubuntu开机自动挂载的ntfs硬盘的权限问题
原文地址:ubuntu开机自动挂载的ntfs硬盘的权限问题 在linux操作系统中, 挂载是一个非常重要的功能,使用非常频繁. 它指将一个设备(通常是存储设备)挂接到一个已存在的目录上. (这个目录可 ...