Codeforces Round #350 (Div. 2) F. Restore a Number 模拟构造题
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).
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.
Print the smalles integer n which Vasya could pass to Kate.
003512
021
30021
#include<bits/stdc++.h>
using namespace std;
const int N = 3e6+, M = 1e6+, mod = 1e9+, inf = 1e9+;
typedef long long ll; int H[N];
vector<string > ans;
char a[N],sub[N];
int b[N],Sub,y[];
int pushdown(int len) {
for(int i=;i<=;i++) H[i]-=y[i];
int tmp = len,can = ;
while(tmp) {
if(H[tmp%]) H[tmp%] = H[tmp%]- ;
else {can = ;H[tmp%] = H[tmp%]- ;}
tmp/=;
}
int sum = ;for(int i=;i<=;i++) H[i]+=y[i];
for(int i=;i<=;i++) sum+=H[i]; if(sum!=len||!can) {
tmp = len;
while(tmp) {
H[tmp%] = H[tmp%] + ;
tmp/=;
}
return ;
}
else return ;
} int main() {
scanf("%s",a+);
int L = strlen(a+);
getchar();gets(sub+);
Sub = strlen(sub+);
for(int i=;i<=L;i++) H[a[i]-'']++;
if(L==&&H[]==&&H[]==) {printf("0\n");return ;}
for(int i=;i<=Sub;i++) y[sub[i]-'']++;
for(int len = ;;len++) {
if(!pushdown(len))continue; for(int i=;i<=;i++) H[i]-=y[i];
int fir ,cnt = ;
for(int i=;i<=;i++) {
for(int j=;j<=H[i];j++) {
b[++cnt] = i;
}
}
//如果全部为0的情况
if(b[cnt]==&&sub[]!='') {
for(int i=;i<=Sub;i++) printf("%c",sub[i]);
for(int i=;i<=cnt;i++) printf("%c",b[i]+'');
}
else {//找到Sub对应的位置就好了
// cout<<1<<endl;
for(int i=;i<=cnt;i++) {
if(b[i]) {
swap(b[],b[i]);
break;
}
}
if(Sub==) {
for(int i=;i<=cnt;i++) printf("%c",b[i]+'');
return ;
} int f = -;
for(int j=;j<=Sub;j++) {
if(sub[j]>sub[j-]) {
f=;break;
}
else if(sub[j]<sub[j-]) {f=;break;}
else continue;
}
int yes = ;
for(int i=;i<=cnt;) {
if(i==&&sub[]!='') {
int l = ,can = ;
while(l<=Sub&&l<=cnt) {
if(b[l]+''<sub[l]) {can = ;break;}
else if(b[l]+''>sub[l]) {can = ;break;}
else {l++;}
}
if(can) {
cout<<sub+;
sort(b+,b+cnt+);
yes = ;
}
printf("%c",b[i++]+'');continue;
}
else if(i==){
printf("%c",b[i++]+'');
continue;
}
if(!yes) printf("%c",b[i++]+'');
else {
int l = i;
int tmp = ;
while(l<=cnt&&b[l]+''<sub[tmp]) {
printf("%c",b[l++]+'');
} if(f<=) {
printf("%s",sub+);
}
else {
while(l<=cnt&&b[l]+''==sub[tmp]) {
printf("%c",b[l++]+'');
}
printf("%s",sub+);
}
yes = ;
i = l;
}
}
if(yes) cout<<sub+;
} // cout<<" "<<len<<endl;
printf("\n");return ;
}
return ;
}
Codeforces Round #350 (Div. 2) F. Restore a Number 模拟构造题的更多相关文章
- Codeforces Round #523 (Div. 2) F. Katya and Segments Sets (交互题+思维)
https://codeforces.com/contest/1061/problem/F 题意 假设存在一颗完全k叉树(n<=1e5),允许你进行最多(n*60)次询问,然后输出这棵树的根,每 ...
- Codeforces Round #237 (Div. 2) C. Restore Graph(水构造)
题目大意 一个含有 n 个顶点的无向图,顶点编号为 1~n.给出一个距离数组:d[i] 表示顶点 i 距离图中某个定点的最短距离.这个图有个限制:每个点的度不能超过 k 现在,请构造一个这样的无向图, ...
- Codeforces Round #350 (Div. 2) A B C D1 D2 水题【D2 【二分+枚举】好题】
A. Holidays 题意:一个星球 五天工作,两天休息.给你一个1e6的数字n,问你最少和最多休息几天.思路:我居然写成模拟题QAQ. #include<bits/stdc++.h> ...
- Codeforces Round #384 (Div. 2) C. Vladik and fractions(构造题)
传送门 Description Vladik and Chloe decided to determine who of them is better at math. Vladik claimed ...
- Codeforces Round #485 (Div. 2) F. AND Graph
Codeforces Round #485 (Div. 2) F. AND Graph 题目连接: http://codeforces.com/contest/987/problem/F Descri ...
- Codeforces Round #486 (Div. 3) F. Rain and Umbrellas
Codeforces Round #486 (Div. 3) F. Rain and Umbrellas 题目连接: http://codeforces.com/group/T0ITBvoeEx/co ...
- Codeforces Round #501 (Div. 3) F. Bracket Substring
题目链接 Codeforces Round #501 (Div. 3) F. Bracket Substring 题解 官方题解 http://codeforces.com/blog/entry/60 ...
- Codeforces Round #499 (Div. 1) F. Tree
Codeforces Round #499 (Div. 1) F. Tree 题目链接 \(\rm CodeForces\):https://codeforces.com/contest/1010/p ...
- DP+埃氏筛法 Codeforces Round #304 (Div. 2) D. Soldier and Number Game
题目传送门 /* 题意:b+1,b+2,...,a 所有数的素数个数和 DP+埃氏筛法:dp[i] 记录i的素数个数和,若i是素数,则为1:否则它可以从一个数乘以素数递推过来 最后改为i之前所有素数个 ...
随机推荐
- WPF获取鼠标当前位置
/// <summary> /// 设置鼠标的坐标 /// </summary> /// <param name="x">横坐标</par ...
- Google搜索命令语法大全
以下是目前所有的Google搜索命令语法,它不同于Google的帮助文档,因为这里介绍 了几个Google不推荐使用的命令语法.大多数的Google搜索命令语法有它特有的使用格式,希望大家能正确使用. ...
- lnmp一键安装包删除添加的域名
lnmp一键安装包删除添加的域名 如果使用lnmp一键安装包/root/vhost.sh 添加的域名可以,可以删除/usr/local/nginx/conf/vhost/要删除的域名.conf 文件, ...
- [整理]通过AngularJS directive对bootstrap日期控件的的简单包装
最近项目上了AngularJS,而原来使用的日期控件的使用方式也需要改变,于是开始了倒腾,看了官方的例子,可以使用AngularJS的directive做简单的处理,这样在html里直接使用申明的的形 ...
- L17 怎么向应用程序商店提交应用
原地址:https://developer.apple.com/library/ios/#referencelibrary/GettingStarted/RoadMapiOS/ApplicationD ...
- Unity游戏开发之“屏幕截图”
原地址:http://sygame.lofter.com/post/117105_791680 在unity游戏开发中,可能会遇到在游戏中截屏的效果.这儿提供两种截屏方法.(方法二提供显示截图缩略图代 ...
- 在Windows Server 2012 中安装 .NET 3.5 Framework
问题 如今,仍然有许多程序和应用需要依靠.NET 3.5 framework 来运行.在Windows Server 2012中,微软提供了.NET 3.5 和.NET 4.5的安装选项以为你的应用程 ...
- rocksdb 编译安装 日志
Compilation RocksDB's library should be able to compile without any dependency installed, although w ...
- css整个页面离顶部的距离
body { padding:0; margin:0; font-size:12px; line-height:22px; } 说明: 整个页面离顶部的距离是22像素
- mysql lower,upper实现大小写
mysql的lower和uppper函数可以将指定字符串转换为小写和大写 select lower('OutSpringTd') as lowerCase, upper('OutSpringTd') ...