【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 ...
随机推荐
- JavaScript--天猫数量输入框
<!DOCTYPE html> <head> <meta charset="utf-8" /> <title>无标题文档</t ...
- 序列化类型为“System.Data.Entity.DynamicProxies..."对象时检测到循环引用
这是因为EF外键引起的序列化问题. 解决方案: context.Configuration.ProxyCreationEnabled = false; 这里我用的是一个基类控制器用于被继承 返回EF实 ...
- 【Mysql的那些事】数据库之ORM操作
1:ORM的基础操作(必会) <1> all(): 查询所有结果 <2> filter(**kwargs): 它包含了与所给筛选条件相匹配的对象 <3> get(* ...
- Gym - 101962B_Color Changing Sofa
题意:将一个沙发放到一个分成好几个色块(一个字母代表一种颜色)的房间里,要求沙发染成跟所在色块一样的颜色,沙发分成(0,1)两种,0可以染成一种颜色,1可以染成一种颜色(换句话说,沙发最多两种颜色), ...
- qt开发ROS遇到这个问题 find_package(catkin) failed. catkin was neither found in the workspace nor in the CMAKE_PREFIX_PATH...
为了实现用Qt开发ROS界面开发环境,我几乎参阅了网上所有的配置教程,安装了多个版本的qt,在ubuntu14.04和ubuntu16.04上分别进行了配置,最后都成功了.不得不说的是用QTCREAT ...
- Android内核剖析读书笔记(1)—Framework概述
一.Framework组成 1.服务端组成 a.WindowManagerService 决定各窗口的叠放次序.隐藏或者显示窗口 b.ActivityManagerService 管理应用 ...
- JVM参数详细列表
-client :设置JVM使用client模式,特点启动较快(神机不明显(I5/8G/SSD)) -server :设置JVM使用server模式.64位JDK默认启动该模式 -agentlib:l ...
- oracle使用DECODE函数来减少处理时间
使用DECODE函数可以避免重复扫描相同记录或重复连接相同的表. 例如: SELECT COUNT(*),SUM(SAL) FROM EMP WHERE DEPT_NO = 0020 AND ENAM ...
- Libev源码分析09:select突破处理描述符个数的限制
众所周知,Linux下的多路复用函数select采用描述符集表示处理的描述符.描述符集的大小就是它所能处理的最大描述符限制.通常情况下该值为1024,等同于每个进程所能打开的描述符个数. 增大描述符集 ...
- phpmyadmin设置自动登录和取消自动登录
1首先在phpmyadmin安装目录下找到config.sample.inc.php复制一份文件名改为config.inc.php 2打开config.inc.php 找到 $cfg['Serve ...