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之前所有素数个 ...
随机推荐
- Windows 下安装项目管理工具 Redmine 1.1.2
1.InstantRails-2.0-win 下载地址 https://rubyforge.org/frs/?group_id=904 2.redmine1.1.2 下载地址 http://www ...
- 利用memcached构建高性能的Web应用程序(转载)
面临的问题 对于高并发高访问的Web应用程序来说,数据库存取瓶颈一直是个令人头疼的问题.特别当你的程序架构还是建立在单数据库模式,而一个数据池连接数峰 值已经达到500的时候,那你的程序运行离崩溃的边 ...
- Oracle 客户端配置
nstantclient-basic-nt-12.1.0.1.0\instantclient_12_1下面新建NETWORK文件夹,NETWORK下新建ADMIN文件夹,ADMIN下新建tnsname ...
- 导入安全证书到jdk步骤详细说明-原
一.首先要在浏览器打开需要证书的网站,然后把证书下载下来,保存的证书名称随意命名,只要保证唯一性(这个唯一性下文有解释) 二.然后把证书复制到%JAVA_HOME%/jre/bin/路径下,即保证证书 ...
- 几款web开发常用jquery特效代码
特效网:http://www.xwcms.net 1.图片拖动特效http://www.xwcms.net/js/tpdm/32946.html2.弹出层焦点图特效:http://www.xwcms ...
- thinkphp中F方法
我们已经了解了ThinkPHP中的S方法的用法,F方法其实是S方法的一个子集功能,仅用于简单数据缓存,并且只能支持文件形式,不支持缓存有效期,因为采用的是PHP返回方式,所以其效率较S方法较高,因此我 ...
- 第16章 使用Squid部署代理缓存服务
章节概述: 本章节从代理缓存服务的工作原理开始讲起,让读者能够清晰理解正向代理(普通模式.透明模式)与反向代理的作用. 正确的使用Squid服务程序部署代理缓存服务可以有效提升访问静态资源的效率,降低 ...
- OpenCV的基本数据结构
参考:http://www.cnblogs.com/guoqiaojin/p/3176692.html
- Linux生产服务器Shell脚本分享
Linux生产服务器Shell脚本分享 2012-6-6 86市场网 linux 作为一名Linux/unix系统管理员,我经常遇到人问这个问题:shell能做什么?PHP这么强大,为什么不用PHP来 ...
- XmlWriter/XmlReader示例代码
在Silverlight项目中,如果您想最大程度的减少xap包的大小,仅使用默认System.Xml命名空间下提供的功能来实现“XML序列化/反序列化”,恐怕XmlReader/XmlWriter将成 ...