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之前所有素数个 ...
随机推荐
- 使用ymPrompt弹框
使用弹框 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8&q ...
- 字符串模拟赛T1
// source code from laekov for c0x17 #define PRID "bxjl" #include <cstdio> #include ...
- POJ 1442 Black Box
第k大数维护,我推荐Treap..谁用谁知道.... Black Box Time ...
- acdream.A Very Easy Triangle Counting Game(数学推导)
A - A Very Easy Triangle Counting Game Time Limit:1000MS Memory Limit:64000KB 64bit IO Forma ...
- 山东省第四届acm.Rescue The Princess(数学推导)
Rescue The Princess Time Limit: 1 Sec Memory Limit: 128 MB Submit: 412 Solved: 168 [Submit][Status ...
- Hadoop之 hdfs 系统
一.NameNode维护着2张表: 1.文件系统的目录结构,以及元数据信息 2.文件与数据块列表的对应关系 存放在fsimage中,在运行的时候加载到内存中的. 操作日志写到edits中 二.Da ...
- 在C#中开启事务
1.为什么要开启事务: 举一个简单的例子:在银行业务中,有一条记账原则,即又借有贷.为了保证这种原则,每发生一笔银行业务,就必须保证会计账目上借方科目和贷方科目至少个少一笔,并且这两笔要么同时成功,要 ...
- 第20章 使用LNMP架构部署动态网站环境
章节概述: 本章节将从Linux系统的软件安装方式讲起,带领读者分辨RPM软件包与源码安装的区别.并能够理解它们的优缺点. Nginx是一款相当优秀的用于部署动态网站的服务程序,Nginx具有不错的稳 ...
- [POJ1007]DNA Sorting
[POJ1007]DNA Sorting 试题描述 One measure of ``unsortedness'' in a sequence is the number of pairs of en ...
- 谷歌浏览器 DEV Tools
谷歌浏览器如今是Web开发者们所使用的最流行的网页浏览器.伴随每六个星期一次的发布周期和不断扩大的强大的开发功能,Chrome变成了一个必须的工具.大多数可能熟悉关于chorme的许多特点,例如使用c ...