Codeforces Round #350 (Div. 2) F. Restore a number simulated construction question

Source: Internet
Author: User

F. Restore a number

Vasya decided to pass a very large an 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 is passed to Kate. The only thing that Vasya remembers, are a non-empty substring of n (a substring of n is a seq Uence of consecutive digits of the number n).

Vasya knows that there is more than one of the 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 is equal to zero itself (in the case a, single digit 0 is 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.

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
Test Instructions:Give you a string a, a string A is an n string form +n bit string form scrambled to and B is n substring now ask you can constitute the smallest n 10 how littleThe following:The smaller the number of observations, the smaller the n will be, the more we enumerate the digits, and then a bunch of simulations.

#include <bits/stdc++.h>using namespacestd;Const intN = 3e6+ -, M = 1e6+Ten, mod = 1e9+7, INF = 1e9+ +; typedefLong Longll;intH[n];vector<string>ans;CharA[n],sub[n];intb[n],sub,y[1001];intPushdown (intLen) {     for(intI=0; i<=9; i++) h[i]-=Y[i]; intTMP = Len,can =1;  while(TMP) {if(h[tmp%Ten]) h[tmp%Ten] = h[tmp%Ten]-1; Else{can =0; h[tmp%Ten] = h[tmp%Ten]-1;} TMP/=Ten; }    intsum =0; for(intI=0; i<=9; i++) h[i]+=Y[i];  for(intI=0; i<=9; i++) sum+=H[i]; if(sum!=len| |!can) {tmp=Len;  while(TMP) {h[tmp%Ten] = h[tmp%Ten] +1; TMP/=Ten; }        return 0; }    Else return 1;}intMain () {scanf ("%s", A +1); intL = strlen (A +1); GetChar (); Gets (sub+1); Sub= strlen (sub+1);  for(intI=1; i<=l;i++) h[a[i]-'0']++; if(l==2&&h[0]==1&&h[1]==1) {printf ("0\n");return 0;}  for(intI=1; i<=sub;i++) y[sub[i]-'0']++;  for(intLen =1;; len++) {        if(!pushdown (len))Continue;  for(intI=0; i<=9; i++) h[i]-=Y[i]; intFIR, cnt =0;  for(intI=0; i<=9; i++) {             for(intj=1; j<=h[i];j++) {b[++CNT] =i; }        }        //if all of the 0 cases        if(b[cnt]==0&&sub[1]!='0') {             for(intI=1; i<=sub;i++) printf ("%c", Sub[i]);  for(intI=1; i<=cnt;i++) printf ("%c", b[i]+'0'); }        Else{//just find the sub-location .//cout<<1<<endl;             for(intI=1; i<=cnt;i++) {                if(B[i]) {Swap (b[1],b[i]);  Break; }            }            if(sub==0) {                 for(intI=1; i<=cnt;i++) printf ("%c", b[i]+'0'); return 0; }             intf =-1;  for(intj=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; }            intYes =1;  for(intI=1; i<=CNT;) {                if(i==1&&sub[1]!='0') {                    intL =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 {                    intL =i; intTMP =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 simulated construction question

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.