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之前所有素数个 ...
随机推荐
- IIS负载均衡-Application Request Route详解第一篇: ARR介绍(转载)
IIS负载均衡-Application Request Route详解第一篇: ARR介绍 说到负载均衡,相信大家已经不再陌生了,本系列主要介绍在IIS中可以采用的负载均衡的软件:微软的Applica ...
- __stdcall,__cdecl,__fastcall的区别
__stdcall,__cdecl,__fastcall的区别 标签: dll编译器pascalclassimportinitialization 2009-12-09 15:07 10472人阅读 ...
- UVALive 6125 I’ve Got Your Back(gammon) 题解
http://vjudge.net/problem/viewProblem.action?id=37481 East Central Regional Contest Problem D: I’ve ...
- [原] Android持续优化 - 提高流畅度
一.形象的感官一下流畅度概念 1. 这是官方给出的概念:Android流畅运行,需要运行60帧/秒, 则需要每帧的处理时间不超过16ms. 2. 每秒帧数,实际上就是指动画或视频每秒放映的画面数.因此 ...
- 老项目的#iPhone6于iPhone6Plus适配#iPhone6分辨率与适配
技术博客http://www.cnblogs.com/ChenYilong/ 本文永久地址为http://www.cnblogs.com/ChenYilong/p/4011744.html ,转 ...
- java类的加载过程
1.类的加载顺序 (1)JVM在首次加载类时会对 静态初始化块.静态成员变量. 静态方法进行一次初始化. (2)只有在调用new方法时才会创建类的实例. (3)对象创建过程: 首先执行父类(如果有) ...
- poj2632 模拟
Crashing Robots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8388 Accepted: 3631 D ...
- BZOJ 1602 USACO2008 Oct 牧场行走
翻翻吴大神的刷题记录翻到的... 乍一看是一个树链剖分吓瓜我...难不成吴大神14-10-28就会了树剖?orz... 再一看SB暴力都可过... 然后一看直接树上倍增码个就好了... 人生真是充满着 ...
- Fast Power
Calculate the a^n % b where a, b and n are all 32bit integers. Example For 2^31 % 3 = 2 For 100^1000 ...
- BestCoder9 1003 Revenge of kNN(hdu 4995) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4995 题目意思:在一个一维坐标轴上,给出位置 xi 和值 vi,对于 M 次询问,每次询问给出inde ...