Codeforces Round #350 (Div. 2) F. Restore a Number 模拟构造题

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

003512021

output

30021

题意:

  

  给你一个字符串a,b

  字符串a是由  n字符串形式+n位数字符串形式 打乱的到的

  且b是n的子串

  现在问你能构成最小的n十多少

题解:

  

  观察到位数越小,n则会越小

  我们枚举位数就好

  然后就是一堆模拟

#include<bits/stdc++.h>
using namespace std;
const int N = 3e6+20, M = 1e6+10, mod = 1e9+7, inf = 1e9+1000;
typedef long long ll;

int H[N];
vector<string > ans;
char a[N],sub[N];
int b[N],Sub,y[1001];
int pushdown(int len) {
    for(int i=0;i<=9;i++) H[i]-=y[i];
    int tmp = len,can = 1;
        while(tmp) {
            if(H[tmp%10]) H[tmp%10] = H[tmp%10]- 1;
            else {can = 0;H[tmp%10] = H[tmp%10]- 1;}
            tmp/=10;
    }
    int sum = 0;for(int i=0;i<=9;i++) H[i]+=y[i];
    for(int i=0;i<=9;i++) sum+=H[i];

    if(sum!=len||!can) {
        tmp = len;
        while(tmp) {
            H[tmp%10] = H[tmp%10] + 1;
            tmp/=10;
        }
        return 0;
    }
    else return 1;
}

int main() {
    scanf("%s",a+1);
    int L = strlen(a+1);
    getchar();gets(sub+1);
    Sub = strlen(sub+1);
    for(int i=1;i<=L;i++) H[a[i]-‘0‘]++;
    if(L==2&&H[0]==1&&H[1]==1) {printf("0\n");return 0;}
    for(int i=1;i<=Sub;i++) y[sub[i]-‘0‘]++;
    for(int len = 1;;len++) {
        if(!pushdown(len))continue;

           for(int i=0;i<=9;i++) H[i]-=y[i];
        int fir ,cnt = 0;
        for(int i=0;i<=9;i++) {
            for(int j=1;j<=H[i];j++) {
                b[++cnt] = i;
            }
        }
        //如果全部为0的情况
        if(b[cnt]==0&&sub[1]!=‘0‘) {
            for(int i=1;i<=Sub;i++) printf("%c",sub[i]);
            for(int i=1;i<=cnt;i++) printf("%c",b[i]+‘0‘);
        }
        else {//找到Sub对应的位置就好了
           //     cout<<1<<endl;
            for(int i=1;i<=cnt;i++) {
                if(b[i]) {
                    swap(b[1],b[i]);
                    break;
                }
            }
            if(Sub==0) {
                for(int i=1;i<=cnt;i++) printf("%c",b[i]+‘0‘);
                return 0;
            }

             int f = -1;
                    for(int j=2;j<=Sub;j++) {
                        if(sub[j]>sub[j-1]) {
                            f=1;break;
                        }
                        else if(sub[j]<sub[j-1]) {f=0;break;}
                        else continue;
                    }
            int yes = 1;
            for(int i=1;i<=cnt;) {
                if(i==1&&sub[1]!=‘0‘) {
                    int l = 1,can = 1;
                    while(l<=Sub&&l<=cnt) {
                        if(b[l]+‘0‘<sub[l]) {can = 0;break;}
                        else if(b[l]+‘0‘>sub[l]) {can = 1;break;}
                        else {l++;}
                    }
                    if(can) {
                        cout<<sub+1;
                        sort(b+1,b+cnt+1);
                        yes = 0;
                    }
                     printf("%c",b[i++]+‘0‘);continue;
                }
                else if(i==1){
                    printf("%c",b[i++]+‘0‘);
                    continue;
                }
                if(!yes) printf("%c",b[i++]+‘0‘);
                else {
                    int l = i;
                    int tmp = 1;
                    while(l<=cnt&&b[l]+‘0‘<sub[tmp]) {
                        printf("%c",b[l++]+‘0‘);
                    }

                    if(f<=0) {
                        printf("%s",sub+1);
                    }
                    else {
                        while(l<=cnt&&b[l]+‘0‘==sub[tmp]) {
                         printf("%c",b[l++]+‘0‘);
                        }
                         printf("%s",sub+1);
                    }
                    yes = 0;
                    i = l;
                }
            }
            if(yes) cout<<sub+1;
        }

       // cout<<" "<<len<<endl;
        printf("\n");return 0;
    }
    return 0;
}
时间: 2024-08-25 06:41:52

Codeforces Round #350 (Div. 2) F. Restore a Number 模拟构造题的相关文章

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

Codeforces Round #486 (Div. 3) F. Rain and Umbrellas 题目连接: http://codeforces.com/group/T0ITBvoeEx/contest/988/problem/E Description Polycarp lives on a coordinate line at the point x=0. He goes to his friend that lives at the point x=a. Polycarp can

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

题目链接 Codeforces Round #501 (Div. 3) F. Bracket Substring 题解 官方题解 http://codeforces.com/blog/entry/60949 ....看不懂 设dp[i][j][l]表示前i位,左括号-右括号=j,匹配到l了 状态转移,枚举下一个要填的括号,用next数组求状态的l,分别转移 代码 #include<bits/stdc++.h> using namespace std; const int maxn = 207;

Codeforces Round #258 (Div. 2) B. Sort the Array(简单题)

题目链接:http://codeforces.com/contest/451/problem/B ---------------------------------------------------------------------------------------------------------------------------------------------------------- 欢迎光临天资小屋:http://user.qzone.qq.com/593830943/ma

Codeforces Round #417 (Div. 2) A. Sagheer and Crossroads 模拟 枚举

Codeforces Round #417 (Div. 2) A. Sagheer and Crossroads 模拟  枚举 题意 一个红绿灯 按逆时针方向一次给出各个路口的左转,直行,右转,以及行人车道让你判断,汽车是否有可能撞到行人 注意 当前车道的左转有可能撞到别的车道的行人的 题解 一大堆特判 1 #include <cstdio> 2 #include <cmath> 3 #include <cstdlib> 4 #include <cstring&g

数学+DP Codeforces Round #304 (Div. 2) D. Soldier and Number Game

题目传送门 1 /* 2 题意:这题就是求b+1到a的因子个数和. 3 数学+DP:a[i]保存i的最小因子,dp[i] = dp[i/a[i]] +1;再来一个前缀和 4 */ 5 /************************************************ 6 Author :Running_Time 7 Created Time :2015-8-1 14:08:34 8 File Name :B.cpp 9 ******************************

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

题目传送门 1 /* 2 题意:b+1,b+2,...,a 所有数的素数个数和 3 DP+埃氏筛法:dp[i] 记录i的素数个数和,若i是素数,则为1:否则它可以从一个数乘以素数递推过来 4 最后改为i之前所有素数个数和,那么ans = dp[a] - dp[b]: 5 详细解释:http://blog.csdn.net/catglory/article/details/45932593 6 */ 7 #include <cstdio> 8 #include <algorithm>

Codeforces Round #404 (Div. 2) C 二分,水 D 数学,好题 E 分块

Codeforces Round #404 (Div. 2) C. Anton and Fairy Tale 题意:仓库容量n,每天运来m粮食,第 i 天被吃 i 粮食,问第几天仓库第一次空掉. tags:==SB题 注:二分边界判断,数据范围爆long long判断. // CF404 C #include<bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000&

Codeforces Round #392 (Div. 2) F. Geometrical Progression

原题地址:http://codeforces.com/contest/758/problem/F F. Geometrical Progression time limit per test 4 seconds memory limit per test 256 megabytes input standard input output standard output For given n, l and r find the number of distinct geometrical pro

Codeforces Round #350 (Div. 2) 题解

Holidays Game of Robots Cinema Magic Powder - 12 Correct Bracket Sequence Editor Restore a Number Holidays #include<bits/stdc++.h> using namespace std; #define For(i,n) for(int i=1;i<=n;i++) #define Fork(i,k,n) for(int i=k;i<=n;i++) #define Re