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).

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
 
题意:
  
  给你一个字符串a,b
  字符串a是由  n字符串形式+n位数字符串形式 打乱的到的
  且b是n的子串
  现在问你能构成最小的n十多少
 
题解:
  
  观察到位数越小,n则会越小
  我们枚举位数就好
  然后就是一堆模拟
 
#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 模拟构造题的更多相关文章

  1. Codeforces Round #523 (Div. 2) F. Katya and Segments Sets (交互题+思维)

    https://codeforces.com/contest/1061/problem/F 题意 假设存在一颗完全k叉树(n<=1e5),允许你进行最多(n*60)次询问,然后输出这棵树的根,每 ...

  2. Codeforces Round #237 (Div. 2) C. Restore Graph(水构造)

    题目大意 一个含有 n 个顶点的无向图,顶点编号为 1~n.给出一个距离数组:d[i] 表示顶点 i 距离图中某个定点的最短距离.这个图有个限制:每个点的度不能超过 k 现在,请构造一个这样的无向图, ...

  3. Codeforces Round #350 (Div. 2) A B C D1 D2 水题【D2 【二分+枚举】好题】

    A. Holidays 题意:一个星球 五天工作,两天休息.给你一个1e6的数字n,问你最少和最多休息几天.思路:我居然写成模拟题QAQ. #include<bits/stdc++.h> ...

  4. 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 ...

  5. Codeforces Round #485 (Div. 2) F. AND Graph

    Codeforces Round #485 (Div. 2) F. AND Graph 题目连接: http://codeforces.com/contest/987/problem/F Descri ...

  6. Codeforces Round #486 (Div. 3) F. Rain and Umbrellas

    Codeforces Round #486 (Div. 3) F. Rain and Umbrellas 题目连接: http://codeforces.com/group/T0ITBvoeEx/co ...

  7. Codeforces Round #501 (Div. 3) F. Bracket Substring

    题目链接 Codeforces Round #501 (Div. 3) F. Bracket Substring 题解 官方题解 http://codeforces.com/blog/entry/60 ...

  8. Codeforces Round #499 (Div. 1) F. Tree

    Codeforces Round #499 (Div. 1) F. Tree 题目链接 \(\rm CodeForces\):https://codeforces.com/contest/1010/p ...

  9. DP+埃氏筛法 Codeforces Round #304 (Div. 2) D. Soldier and Number Game

    题目传送门 /* 题意:b+1,b+2,...,a 所有数的素数个数和 DP+埃氏筛法:dp[i] 记录i的素数个数和,若i是素数,则为1:否则它可以从一个数乘以素数递推过来 最后改为i之前所有素数个 ...

随机推荐

  1. 一个C++类的注释:

    #ifndef __RUNTIMEPARA__HPP#define __RUNTIMEPARA__HPP #include <string> //后面会有介绍 #include <m ...

  2. POJ 1054 The Troublesome Frog

    The Troublesome Frog Time Limit: 5000MS Memory Limit: 100000K Total Submissions: 9581 Accepted: 2883 ...

  3. 3月23.CSS表格布局

    360表格布局: CSS定义标签: @charset "utf-8";/* CSS Document */.bt1{ border:#309 solid 1px; height:1 ...

  4. Power Network(网络流最大流 & dinic算法 + 优化)

    Power Network Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 24019   Accepted: 12540 D ...

  5. Python字符串基础操作

    ==============字符串======== >>> s1='www.baidu.com' >>> type(s1) <type 'str'> & ...

  6. HNU 12833 Omar’s Bug(分情况讨论)

    题目链接:http://acm.hnu.cn/online/?action=problem&type=show&id=12833&courseid=268 解题报告:有个11个 ...

  7. ios数据库

    1. ios数据库管理软件 ios使用的数据库是sqlite 管理软件有2种, 我只记得一种, 名字叫做 MesaSQLite 2. sqlite数据库 2.1.修改表结构 ①:更改字段类型长度 AL ...

  8. ubuntu apt-get update 失败解决

    在执行 sudo apt-get update 之后  会出现如下错误 这是要检测网络是否有问题 因为我之前只使用nfs挂载的时候,在虚拟机  编辑-> 虚拟网络编辑器里面->更改设置里面 ...

  9. VIM中的折叠命令

    参考:http://blog.csdn.net/bruce0532/article/details/8497284 za:在折叠与展开间自由转换 zf:创建折叠 示例:zf 3j    #本行及以下3 ...

  10. 设置windows网络连接别名和linux网络连接别名

    windows网络连接别名 C:\Windows\System32\drivers\etc目录下的hosts文件中添加 127.0.0.1 localhost 192.168.1.100 proxy. ...