码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2016-05-11 21:45:25      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:

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+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;
}

 

 

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

标签:

原文地址:http://www.cnblogs.com/zxhl/p/5483271.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!